counting frequency of strings

Hi I have a very large variable containing 3 letter country codes (GBR, CHN, USA, etc) and i think some of them repeat, so what is the best way to find the frequency of every country in that variable?
Thanks PS: variable size is 100 x 3 (100 countries)

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 22일
편집: Azzi Abdelmalek 2013년 3월 22일

3 개 추천

x=strvcat('USA','ALG','FRA','ALG','FRA');
y=unique(x,'rows');
for k=1:size(y,1)
freq(k)=sum(ismember(x,y(k,:),'rows'));
end
out=[cellstr(y) num2cell(freq')]
If your data is a cell array
x={'USA';'ALG';'FRA';'ALG';'FRA'};
y=unique(x);
for k=1:numel(y)
freq(k)=sum(strcmp(x,y(k)));
end
out=[y num2cell(freq')]

댓글 수: 2

huehuehue
huehuehue 2013년 3월 23일
exactly what I wanted, thanks!
Cedric
Cedric 2013년 3월 23일
편집: Cedric 2013년 3월 23일
Don't forget to [ Accept ] the answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

질문:

2013년 3월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by