Count the number of the same rows in array
古いコメントを表示
I have array A and I want to count number of occurence the same rows in array
A=[ 44444; 66666; 44444; 88888; 44444; 66666]
Result should be like output.
output:
44444 3
66666 2
88888 1
As you can see row 44444 occured 3 times in array A,66666 2 times and 88888 one time.
The best will be use only matlab function instead of loops.
回答 (1 件)
Star Strider
2017 年 9 月 7 日
Try this:
A=[ 44444; 66666; 44444; 88888; 44444; 66666];
[Au,~,ic] = unique(A, 'stable');
tally = accumarray(ic, 1);
Result = [Au tally]
Result =
44444 3
66666 2
88888 1
The unique function finds the unique value and returns a vector (here ‘ic’) that are the indices of the unique values in ‘A’. The accumarray function counts and produces as output a vector of the number of occurrences of the unique values. The ‘Result’ matrix horizontally concatenates the unique values and the ‘tally’ vector.
カテゴリ
ヘルプ センター および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!