Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一个函数来查找 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]'.)
[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
答案可能不对。试试 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