find most frequent characters in a string

4 ビュー (過去 30 日間)
Alex
Alex 2011 年 12 月 13 日
I have a string and I want to find the most frequent characters that appear in it. Is there anyway to do this with matlab?

採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 13 日
mode()
  3 件のコメント
David Young
David Young 2011 年 12 月 13 日
char(mode(double(str)))
Walter Roberson
Walter Roberson 2011 年 12 月 13 日
David's code is fine. It could also be written more concisely as
char(mode(0+str))

サインインしてコメントする。

その他の回答 (1 件)

David Young
David Young 2011 年 12 月 13 日
One way to get the commonest n characters, in descending order of frequency:
>> str = 'hello world';
>> n = 5; % number of characters to report
>> [~, c] = sort(hist(double(str), 0:255), 'descend');
>> f = char(c(1:n)-1)
f =
lo de
There may well be numerous better ways.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by