Count the occurrence of each element in an array

2 ビュー (過去 30 日間)
Ben Nguyen
Ben Nguyen 2022 年 9 月 23 日
編集済み: Matt J 2022 年 9 月 23 日
Hi, Im new to MATLAB. I would like to see if there is a way to count the occrurrence of each the element in an array. For example:
string = 'I am happy';
ch = char(string)
ans = 10×1 char array
'I'
' '
'a'
'm'
' '
'h'
'a'
'p'
'p'
'y'
I want to count the number of ocurrence of each element. For example:
['I' , 1] or ['a' , 2] or ['p' , 2] or [' ', 2]
and maybe store the reuslt in another array. Hope it makes sense. Any help or guide will be appreciate!

採用された回答

Matt J
Matt J 2022 年 9 月 23 日
編集済み: Matt J 2022 年 9 月 23 日
string = 'I am happy';
[N,e]=histcounts(double(string),32:130);
table(char(e(N>0)'),N(N>0)','VariableNames',{'Character','Count'})
ans = 7×2 table
Character Count _________ _____ 2 I 1 a 2 h 1 m 1 p 2 y 1

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by