finding most common letter in a bunch of words

조회 수: 1 (최근 30일)
Max
Max 2015년 11월 10일
댓글: Guillaume 2015년 11월 10일
say I have a bunch of words
x={'abac';'abaca';'abacate';'abacay';'abacinate';'abacination';'abaciscus'}
What would I write to find the most common letter in x so for example Most_common_letter=%most common letter that appears in x

채택된 답변

Stephen23
Stephen23 2015년 11월 10일
편집: Stephen23 2015년 11월 10일
It is easy using mode:
>> x={'abac';'abaca';'abacate';'abacay';'abacinate';'abacination';'abaciscus'};
>> char(mode(+[x{:}]))
ans = a

추가 답변 (2개)

Guillaume
Guillaume 2015년 11월 10일
Two steps are required:
  1. build the histogram of the letters, however you want (using accumarray, histc or histcounts)
  2. find the max of the histogram
x = {'abac';'abaca';'abacate';'abacay';'abacinate';'abacination';'abaciscus'};
[letter, ~, pos] = unique([x{:}]);
letterhist = accumarray(pos, 1); %letter histogram
[~, maxidx] = max(letterhist);
fprintf('the most frequent letter is: %c\n', letter(maxidx));
  댓글 수: 2
Max
Max 2015년 11월 10일
I dont want to print the value, is it possible to just have most_common_letter='a' as an answer?
Guillaume
Guillaume 2015년 11월 10일
The printing was just for demo. Getting the letter is there right in the code:
letter(maxidx)

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


Thorsten
Thorsten 2015년 11월 10일
편집: Thorsten 2015년 11월 10일
x={'abac';'abaca';'abacate';'abacay';'abacinate';'abacination';'abaciscus'};
[h c] = hist(double([x{:}]), double('a':'z'))
[~, idx] = max(h);
char(c(idx))

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by