# Histogram Counts of a 2d Matrix

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.

• General programming questions are off-topic here, but can be asked on Stack Overflow. – Marcus Müller Nov 7 '17 at 20:42
• by the way, actually stating how large "very" large is would be interesting - is it more like 1000x2, are the values integers like in your example, what are the possible values for each of these values etc. (toy examples to clarify a problem are very fine, but when it comes to performance, only an actual problem statement in the same order of complexity as your actual problem will cut it). – Marcus Müller Nov 7 '17 at 22:00
• Sorry for the confusion, I am editing to make it more clear. – Archura Nov 7 '17 at 22:10

## 2 Answers

Are you looking for a specific function to perform this task? Otherwise this could probably be done with a simple for loop.

Determine your unique rows using the function

bins = unique(A, 'rows');

Then just loop through each row of A and count each instance of each bin you find in A.

This is probably good enough if you dont care about how long the program runs (which still isnt long at all if your A matrix isn't ridiculously large), but keep in mind MATLAB doesn't "like" running loops.

• Yes, I have actually used bins = unique(A, 'rows') function, the matrix I want to search is massive. So I don't want to use a loop. – Archura Nov 7 '17 at 20:54
• I can think of one way to do this, but will require looping through bins. Hopefully that matrix is not terribly large. Let's call each row in bins, 'b'. The following line should give you the number of times the row 'b' appears in the matrix A: > sum(sum(A == b, 2) == 2); This works because A == b gives you an 'A' sized matrix showing which values match b. We only care about the rows where both columns match b, so we sum across the columns (giving us 2 wherever there are two matches). Finally just sum the number of times we get 2; this is equivalent to counting the number of rows. – goldrik Nov 7 '17 at 21:00
• I am sorry to say that, my matrix of the statistics is very large, and actually I am looking for top the 30 most occurred rows of it. I only kept my original answer for such a small matrix to make it simple to understand. Your contribution is much appreciated. – Archura Nov 7 '17 at 21:03
• Try using the function accumarray(A, 1) – goldrik Nov 7 '17 at 21:12

The consolidator function by J. d'Errico (a solid contributor to MatlabCentral, with heavily optimized and versatile code) can be quite useful here. For your example:

nTopRepeat = 3;
[xA,yA] = consolidator(A,[],'count');
[~,idx] = sort(yA,'descend');
[xA(idx(1:nTopRepeat),:), yA(idx(1:nTopRepeat),:)]


I have just tried it with a large matrix as you suggest:

A = round(randn(565000,2));
nTopRepeat = 30;
[xA,yA] = consolidator(A,[],'count');
[~,idx] = sort(yA,'descend');
[xA(idx(1:nTopRepeat),:), yA(idx(1:nTopRepeat),:)]


This takes 0.3 seconds here. And it scales quite linearly even with $100$ times more rows. And it does loop, over replicates only.

• Thank you very much dear Sir, I as well have just edited my question with the answer I have found myself. I believe this is a neat alternative. – Archura Nov 13 '17 at 20:37
• I have edited and added my own values. – Archura Nov 14 '17 at 14:48
• Are there the same with a random matrix like A = round(randn(565000,2)); – Laurent Duval Nov 14 '17 at 14:53
• Yes, and the elements of A are generally between -25 and 25. – Archura Nov 14 '17 at 16:23