简体   繁体   English

在矩阵的单元格数组中查找元素的索引

[英]Find index of element in cell array of matrices

I want to find the index(that's in row and column) of a specific matrix in a cell array of matrices, for example if I have 我想在矩阵的单元格数组中找到特定矩阵的索引(在行和列中),例如,如果我有

A = [2 3;4 1]

and

B = {[2 2;1 1] [2 3;4 1] [1 1;1 1]}

then I want to return 2 (because B{2}==A ). 那么我想返回2 (因为B{2}==A )。

I want to solve this without for , although I don't have to, the cell array is basically small, but I want to do it without for anyway. 我想解决这个问题,而不for ,虽然我没有到,单元阵列基本上是小,但我想这样做不for呢。

I searched for this and found this and this on SO but their solution only work for strings which I don't have here. 我搜索了这一点,并发现对SO,但他们的解决方案只针对那些没有在这里我字符串的工作。

So how to solve this without for -loop ? 那么如何在没有for循环的情况下解决这个问题呢?

Note 注意

A is an ordinary matrix not a single element cell array, B is a cell array of matrices. A是普通矩阵而不是单元素单元格数组, B是矩阵的单元格数组。

Some possibilities: 一些可能性:

  • Use cellfun with isequal to test each element of B for equality: 使用带有isequal cellfun来测试B每个元素是否相等:

     find(cellfun(@(x) isequal(x,A), B)) 
  • If all matrices have the same size : concatenate into a 3D array (or better yet, use a 3D array from the beginning), and use bsxfun for the comparison: 如果所有矩阵的大小相同 :串联成3D数组(或者更好的是,从头开始使用3D数组),然后使用bsxfun进行比较:

     find(all(all(bsxfun(@eq, A, cat(3, B{:})),1),2)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM