5

我正在寻找一个函数来查找 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]'.)

有谁知道这样的功能?

4

2 回答 2

14

您可以使用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
于 2011-01-24T15:19:36.520 回答
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
于 2011-11-07T01:50:16.623 回答