2011-01-24 136 views
5

我正在寻找一种函数来查找MATLAB中矩阵中最重复(即模态)的行。喜欢的东西:在MATLAB矩阵中查找最重复的行

>> A = [0, 1; 2, 3; 0, 1; 3, 4] 

A = 

0  1 
2  3 
0  1 
3  4 

然后运行:

>> mode(A, 'rows') 

将与第二输出给该行发生时的指标返回[0, 1],理想情况下(即[1, 3]'

有谁知道这样的一个函数?

回答

13

您可以使用UNIQUE获取唯一的行索引,然后在其上调用MODE

[uA,~,uIdx] = unique(A,'rows'); 
modeIdx = mode(uIdx); 
modeRow = uA(modeIdx,:) %# the first output argument 
whereIdx = find(uIdx==modeIdx) %# the second output argument 
+1

谢谢。我认为最后一行应该是这样的:`whereIdx = find(uIdx(modeIdx)== uIdx)`虽然。 – 2011-01-24 16:48:45

2

答案可能不对。试试A = [2,3; 0,1; 3,4; 0,1]。 它应该是以下内容:

[a, b, uIdx] = unique(A,'rows'); 
modeIdx = mode(uIdx); 
modeRow = a(modeIdx,:) %# the first output argument 
whereIdx = find(ismember(A, modeRow, 'rows')) %# the second output argument