MATLAB Answers

# plotting group size vs total member in group of particular size

1 view (last 30 days)
Maggie liu on 16 Jun 2021
Commented: Maggie liu on 16 Jun 2021
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!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

dpb on 16 Jun 2021
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
##### 1 CommentShowHide None
Maggie liu on 16 Jun 2021
thank you :)

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by