Count occurences of row in matrix faster than by using nnz

3 ビュー (過去 30 日間)
RR
RR 2022 年 7 月 20 日
コメント済み: Bruno Luong 2022 年 7 月 20 日
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 件のコメント
DGM
DGM 2022 年 7 月 20 日
Thanks @Bruno Luong
I really wish we could upvote comments ...
Bruno Luong
Bruno Luong 2022 年 7 月 20 日
@DGM you just did ;-)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating 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