[英]How can I count number of occurrences of unique row in MATLAB ?
我有一个如下的矩阵,
A =
1 2 3
4 5 6
7 8 9
10 11 12
4 5 6
7 8 9
4 5 6
1 2 3
我可以使用命令A_unique = unique(A,'rows')
提取此矩阵中的A_unique = unique(A,'rows')
,结果如下
A_unique =
1 2 3
4 5 6
7 8 9
10 11 12
我需要找到每行存在于主矩阵A
的次数
A_unique_count =
2
3
2
1
如何找到唯一行数? 有人可以帮忙吗? 提前致谢
马努
unique
的第三个输出为您提供原始数组中唯一行的索引。 您可以将其与accumarray
一起使用以计算出现的次数。
例如:
A = [1 2 3
4 5 6
7 8 9
10 11 12
4 5 6
7 8 9
4 5 6
1 2 3];
[uniquerow, ~, rowidx] = unique(A, 'rows');
noccurrences = accumarray(rowidx, 1)
返回值:
noccurrences =
2
3
2
1
符合预期
我会推荐@excaza的方法 。 但仅出于多样性:
A_unique_count = diff([0; find([any(diff(sortrows(A), [], 1), 2); 1])]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.