Can I use UNIQUE to get a count of the number of times each element is repeated?
223 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2011 年 4 月 14 日
回答済み: Lola Davidson
2022 年 9 月 6 日
I would like to know if the UNIQUE function has the capability of returning the number of times each element appears in an array.
採用された回答
MathWorks Support Team
2011 年 4 月 14 日
The ability to return the count of each unique element is not available in the UNIQUE function in MATLAB.
To work around this issue, use the ARRAYFUN function to test each element of an array, determining the number of times each element is equal to an element of original vector.
For example:
a = [12 34 78 8 12 3 34 34];
c = arrayfun(@(x)length(find(a == x)), unique(a), 'Uniform', false);
cell2mat(c)
0 件のコメント
その他の回答 (1 件)
Lola Davidson
2022 年 9 月 6 日
>> a = [12 34 78 8 12 3 34 34]';
>> [counts, groupnames] = groupcounts(a)
counts =
1
1
2
3
1
groupnames =
3
8
12
34
78
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!