Matlab - 在矩阵中查找值

时间:2015-03-15 13:35:12

标签: matlab matrix

如何找到包含A中所有值到矩阵B的行,并使用Matlab显示行的索引?

A= [2 5 6];

B=[1 2 4 9 10 15 27 30; 
   1 2 3 4 5 6 7 8; 
   1 2 3 5 6 9 22 101; 
   2 4 5 6 14 20 22 23]

由于

1 个答案:

答案 0 :(得分:3)

3D中的bsxfun -

ind = find(all(any(bsxfun(@eq,B,permute(A,[1 3 2])),2),3))

再次使用bsxfun,但将其保留在2D -

ind = find(sum(reshape(any(bsxfun(@eq,B(:),A(:).'),2),size(B)),2)==numel(A))

使用ismember -

ind = find(sum(reshape(ismember(B(:),A(:)),size(B)),2)==numel(A))

来自pdist2Statistics and Machine Learning Toolbox -

ind = find(sum(reshape(any(pdist2(B(:),A(:))==0,2),size(B)),2)==numel(A))

Statistics and Machine Learning Toolbox再次knnsearch -

[~,dists] = knnsearch(A(:),B(:))
ind = find(sum(reshape(dists==0,size(B)),2)==numel(A))

示例运行 -

A =
     2     5     6
B =
     1     2     4     9    10    15    27    30
     1     2     3     4     5     6     7     8
     1     2     3     5     6     9    22   101
     2     4     5     6    14    20    22    23
ind =
     2
     3
     4