plotting group size vs total member in group of particular size

조회 수: 2 (최근 30일)
Maggie liu
Maggie liu 2021년 6월 16일
댓글: Maggie liu 2021년 6월 16일
Hi I fairly new to MATLAB and would like to know if there's a way to do this without involving too much looping.
so say I have a array that looks like [1 1 1 2 2 3 4 4 5 ]
I want to know the range of sizes of the groups (there's 3 ones, 2 twos, 1 three, 2 fours, 1 five) i.e I would like to have it return [1 2 3]
And I would also like to know the total number of members in each group size. In this case it would be [2 2 1] since two groups have size 1, two groups have size 2 and one gorup has size 3, etc.
Any help is appreciated!

채택된 답변

dpb
dpb 2021년 6월 16일
v =[1 1 1 2 2 3 4 4 5];
% engine
u=unique(v);
ng=histc(v,u);
U=unique(ng)
U =
1 2 3
NU=histc(ng,U)
ans =
2 2 1
histc has been deprecated by TMW in favor ot histcounts, but it doesn't always behave as want it to ... although there's an additional argument to it 'BinMethod','integers' it's just more to remember (I had to go look it up to write this comment) when know precisely how to get what want with the venerable histc

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by