2015-01-12 76 views
0

我有两个矩阵,我想要找到矩阵B中具有矩阵A中相同行值的行的索引。我举一个简单的例子:在matlab中找到矩阵的多个相应行的索引

A=[1,2,3; 2,3,4; 3,5,7; 1,2,3; 1,2,3; 5,8,6]; 
B=[1,2,3; 29,3,4; 3,59,7; 1,29,3; 1,2,3; 5,8,6;1,2,3]; 

例如,对于在矩阵A,该ROW1,ROW5和ROW7第一行矩阵B的对应关系。 我写了下面的代码,但它不返回所有在矩阵A中具有相同行值的索引,并且它们中只有一个(row7)被支持!

A_sorted = sort(A,2,'descend'); % sorting angles 
B_sorted = sort(B,2,'descend'); % sorting angles 
[~,indx]=ismember(A_sorted,B_sorted,'rows') 

结果是

indx_2 = 

7 
0 
0 
7 
7 
6 

它用于在所述第一行中的矩阵A,只有一排(行7)在矩阵B可用!!但你可以在矩阵A看到第一排有矩阵B 3个通讯员行(第1行,第5行和第7行)

回答

1

我认为最好的策略是运用ismember独特的行

%make matrix unique 
[B_unique,B2,B3]=unique(B_sorted,'rows') 
[~,indx]=ismember(A_sorted,B_unique,'rows') 
%For each row in B_unique, get the corresponding indices in B_sorted 
indx2=arrayfun(@(x)find(B3==x),indx,'uni',0) 
+0

THX,这是一个明智的解决方案。它工作正常:) –

+1

@ user3185893不要忘记接受最有帮助的答案。谢谢! –

1

如果你想将所有的行对比较AB之间,使用

E = squeeze(all(bsxfun(@eq, A, permute(B, [3 2 1])), 2)); 

或等价

E = pdist2(A,B)==0; 

在你的榜样,这给

E = 
    1  0  0  0  1  0  1 
    0  0  0  0  0  0  0 
    0  0  0  0  0  0  0 
    1  0  0  0  1  0  1 
    1  0  0  0  1  0  1 
    0  0  0  0  0  1  0 

E(ia,ib)告诉您的Aia个行等于Bib个排。

相关问题