Counting repeated strings in an array

34 次查看(过去 30 天)
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 个评论
the cyclist
the cyclist 2014-11-18
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?

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2014-11-18
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));

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by