查看单个帖子
旧 2019-12-14, 20:13   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 查找MATLAB矩阵中重复次数最多的行

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

更多&回答...
poster 当前离线   回复时引用此帖