I am currently working on a project in which I have a 10 x 2 matrix: A. I want to find the top 3 number of occurrences of each row using MATLAB.
A = [ 1 2; 1 2; 2 4; 3 2; 3 2; 2 4; 1 2; 1 2; 5 8; 3 2]
In return I want to have:
[ 1 2 ]
, 4 times.[ 3 2 ]
, 3 times.[ 2 4 ]
, 2 times.
I have already tried hist3
and similar functions but couldn't get what I wanted.
P.S. My original matrix of statistics is very large (around 565000 x 2 double, depending on the image I analyze) and I am looking for around top 30 most occurred rows of it. In that case, I hardly believe if any loop based answer will cover it.
Edit: I have found the answer yesterday with the help of my professor.
What I have basically done is to set the range for hist3
on x and y axises. Let's say ( -5 : 5 ).
ctrs{1} = -5 : 5;
ctrs{2} = -5 : 5;
hist_A = hist3(A, ctrs);
Then by using sort
function in descending format, I have calculated the occurrences and the indices of the elements of matrix A.
[hist_A_occurrence, hist_A_index] = sort(hist_A(:),'descend');
Finally, to pick the top 3 number of occurrences:
hist_A_index = hist_A_index(1:3);
hist_A_occurrence = hist_A_occurrence(1:3);
Performance:
It was able compute on A
matrix, which is 563160 x 2, for the top 60 values in 0.147038 seconds with a Core i7 4th gen processor and 16gb ram.
The method is very quick, I hope this helps to the others.
Thank you all for the answers.