在MATLAB矩阵中找到重复最多的行

Bil*_*ham 5 matlab row mode matrix

我正在寻找一个函数来在MATLAB中找到矩阵的最重复(即模态)行.就像是:

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

A =

 0     1
 2     3
 0     1
 3     4
Run Code Online (Sandbox Code Playgroud)

然后运行:

>> mode(A, 'rows')
Run Code Online (Sandbox Code Playgroud)

会返回[0, 1],理想情况下会有第二个输出给出该行发生的索引(即[1, 3]'.)

有谁知道这样的功能?

Jon*_*nas 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
Run Code Online (Sandbox Code Playgroud)

  • 这可能不对.试试A = [2,3; 0,1; 3,4; 0,1] (2认同)