Count occurences of row in matrix faster than by using nnz

조회 수: 1(최근 30일)
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));
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 2022년 7월 20일
편집: DGM 2022년 7월 20일
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.
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);
[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.
  댓글 수: 4

댓글을 달려면 로그인하십시오.

추가 답변(0개)





Community Treasure Hunt

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

Start Hunting!

Translated by