Count occurences of row in matrix faster than by using nnz

3 次查看(过去 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));
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
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.
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.
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by