Counting repeated strings in an array

14 views (last 30 days)
Md Mamun Reaz
Md Mamun Reaz on 18 Nov 2014
Answered: the cyclist on 18 Nov 2014
Hi all,
I have an array like below
B b Bb C c Cc A D a AB bD DC ca ABb BbD bDC DCc Cca ABbD BbDC bDCc DCca ABbDC BbDCc bDCca ABbDCc BbDCca ABbDCca BC Cb BCb d aB bd caB aBC Cbd CcaB caBC aBCb BCbd DCcaB CcaBC caBCb aBCbd DCcaBC CcaBCb caBCbd DCcaBCb CcaBCbd DCcaBCbd dc bdc Cbdc AD Da ADa BA Ad DaB aBA BAd DaBA aBAd DaBAd da Ada ab dab BAda Adab BAdab
Now I wanted to see the unique values as well as their corresponding repeated values without sorting the aray. For example:
Output will be unique('A','stable') and their occurance
Thank you
  1 Comment
the cyclist
the cyclist on 18 Nov 2014
Is it one long string, with spaces, like
S = 'B b Bb C and so on'
or is it a cell array
S = {'B','b','Bb','C', and so on}
or something else?

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 18 Nov 2014
I think you can use the third output of unique, along with the hist function:
[uniqueS,~,idx] = unique(S,'stable');
count = hist(idx,unique(idx));

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!