find most frequent characters in a string

4 vues (au cours des 30 derniers jours)
Alex
Alex le 13 Déc 2011
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Déc 2011
mode()
  3 commentaires
David Young
David Young le 13 Déc 2011
char(mode(double(str)))
Walter Roberson
Walter Roberson le 13 Déc 2011
David's code is fine. It could also be written more concisely as
char(mode(0+str))

Connectez-vous pour commenter.

Plus de réponses (1)

David Young
David Young le 13 Déc 2011
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.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by