Find the most duplicate row in the MATLAB matrix

I am looking for a function to find the most repeated (i.e. modal) rows of a matrix in MATLAB. Sort of:

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

A =

 0     1
 2     3
 0     1
 3     4

Then runs:

>> mode(A, 'rows')

will return [0, 1], ideally, the second output giving the indices where this row occurred (i.e [1, 3]'..)

Does anyone know about such a feature?

+5
source share
2 answers

You can use UNIQUE to get unique row indices and then call MODE .

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

The answer may be wrong. Try A = [2, 3; 0, 1; 3, 4; 0, 1]. It should be the following:

[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
+2
source

All Articles