Can I use UNIQUE to get a count of the number of times each element is repeated?

조회 수: 89 (최근 30일)
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
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)

추가 답변 (1개)

Lola Davidson
Lola Davidson 2022년 9월 6일
As of R2019a, you can use groupcounts for this:
>> 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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품


릴리스

R14SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by