找到矩阵中每行的出现次数

时间:2013-04-20 17:27:44

标签: matlab vectorization

我有一个基于行排序的矩阵。例如,矩阵可以如下

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
];

我想在上面的矩阵中找到每个重复行的计数

[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

1 个答案:

答案 0 :(得分:3)

一种解决方案是检索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