登录论坛

查看完整版本 : 查找MATLAB矩阵中重复次数最多的行


poster
2019-12-14, 20:13
我正在寻找一个函数来查找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]' 。

有人知道这样的功能吗?



回答:

您可以使用UNIQUE (http://www.mathworks.com/help/techdoc/ref/unique.html)来获取唯一的行索引,然后对它们调用MODE (http://www.mathworks.com/help/techdoc/ref/mode.html) 。

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

更多&回答... (https://stackoverflow.com/questions/4783564)