简体   繁体   English

查找矩阵中每一行的出现次数

[英]finding the count of occurrence of each row in matrix

I have a matrix that sorts based on its rows. 我有一个根据其行排序的矩阵。 in example the matrix may be as follow 例如矩阵可以如下

M=[
0   3
1   1
1   2
1   2
1   2
1   2
1   3
1   3
2   0
2   2
2   2
2   3
3   2
3   3
3   3
];

I want to find the count of each repetitive row in above matrix 我想在上面的矩阵中找到每个重复行的计数

[0  3] count is 1
[1  1] count is 1
[1  2] count is 4
[1  3] count is 2
[2  0] count is 1
[2  2] count is 2
[2  3] count is 1
[3  2] count is 1
[3  3] count is 2

One solution would be to retrieve unique() occurrences of rows and then accumulate its index: 一种解决方案是检索出现的unique()行,然后累积其索引:

[unM, ~, subs] = unique(M,'rows');
[unM accumarray(subs,1)]
ans =
     0     3     1
     1     1     1
     1     2     4
     1     3     2
     2     0     1
     2     2     2
     2     3     1
     3     2     1
     3     3     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM