我正在寻找一个函数来查找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来获取唯一的行索引,然后对它们调用
MODE 。
[uA,~,uIdx] = unique(A,'rows'); modeIdx = mode(uIdx); modeRow = uA(modeIdx,:) %# the first output argument whereIdx = find(uIdx==modeIdx) %# the second output argument
更多&回答...