# Count occurences of row in matrix faster than by using nnz

1 view (last 30 days)
RR on 20 Jul 2022
Commented: Bruno Luong on 20 Jul 2022
Hi there,
I have an matrix M of about 500k x 4 and I would like to count, how often each row occurs and the output should look like
[M(1,1), M(1,2), M(1,3), number of occurences;
M(2,1), M(2,2), M(2,3), number of occurences;
.... ]
Currently, I am using
for i=1:1:length(M)
M(i,4)=nnz(all(M(:,1:3)==[M(i,1) M(i,2) M(i,3)],2));
end
which does the job but it's very slow with this matrix size. I read a lot about accumarray for this purpose and it's supposed to be much faster but so far my efforts to get it running weren't successful. Could you help me make it work? Or is there maybe an even more suitable function for this job? Thanks so much in advance! :-)

DGM on 20 Jul 2022
Edited: DGM on 20 Jul 2022
If you know there are repeated rows, then you know that you're performing redundant operations. One thing you could do is to use
[C,IA,IC] = unique(A,'rows')
to reduce the size of the set. Then instead of counting instances in A or C, count the instances in IC, since they're all scalars.
Consider:
A = [1 2 3; 4 5 6; 1 2 3; 5 9 3; 1 2 3]
A = 5×3
1 2 3 4 5 6 1 2 3 5 9 3 1 2 3
[C,~,IC] = unique(A,'rows');
urows = size(C,1);
instances = zeros(urows,1);
for r = 1:urows
instances(r) = nnz(IC==r);
end
[C instances]
ans = 3×4
1 2 3 3 4 5 6 1 5 9 3 1
Are there ways to speed up the counting of instances? Probably.
Bruno Luong on 20 Jul 2022
@DGM you just did ;-)

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!