taking second mode from strings

조회 수: 1 (최근 30일)
Max
Max 2015년 11월 11일
댓글: Thorsten 2015년 11월 11일
I want to write something that takes the most common letter from a cell array of it does not appear in another string
newguess=char(mode(+[dict{:}])); %gives the most common letter out of a cell array of words
Say pastguesses='eas' and newguess='e' I want to return the second most common letter until newguess doesn't appear in pastguesses.
I tried something like
if newguess==pastguesses
%take next most common newguess until newguess doesn't appear in pastguesses
end

답변 (1개)

Thorsten
Thorsten 2015년 11월 11일
You can compress your dictionary to a single string of letters and then remove the most common letter from the string. Then find the second most common letter in this string. Note that I used double instead of + to convert from char to double, because it is easier to understand:
dict = {'a' 'b' 'ba' 'c' 'hallo'};
letters = [dict{:}];
mostcommon = char(mode(double(letters)))
newletters = strrep(letters, mostcommon, '')
secondmostcommon = char(mode(double(newletters)))
  댓글 수: 2
Max
Max 2015년 11월 11일
newguess=char(mode(+[dict{:}])); %gives the most common letter out of a cell array of words
I mean is it possible to take the highest appearing letter that is not in pastguesses from example if pastguesses='eai' and newguess='e' I ignore newguess and take the second most common letter so newguess='a' but newguess='a' appears in pastguesses so we take the next most common letter so newguess='i' but it appears in pastguesses so we take the next most common letter say newguess='o' 'o' doesnt appear in pastguesses so we stop at newguess='o'
The newguess is generated from a list of words and pastguesses keeps getting updated in my code. so for example say
dict= {'aa';'aal';'aalii';'aam';'aani';'aardvark';'aardwolf';'aaron';'aaronic';'aaronical';'aaronite';'aaronitic';'aaru';'ab';'aba';'ababdeh';'ababua';'abac';'abaca';'abacate'}
newguess= %is the highest occuring letter in dict that does not appear in pastguesses.
Thanks for the help.
Thorsten
Thorsten 2015년 11월 11일
You remove the letters in passguesses from the letters in your dictionary, and then determine the mode of the remaining letters:
dict= {'aa';'aal';'aalii';'aam';'aani';'aardvark';'aardwolf';'aaron';'aaronic';'aaronical';'aaronite';'aaronitic';'aaru';'ab';'aba';'ababdeh';'ababua';'abac';'abaca';'abacate'}
letters = [dict{:}];
pastguesses='eas'
for i=1:length(pastguesses)
letters = strrep(letters, pastguesses(i), '');
end
mostcommonnotinpastguesses = char(mode(double(letters)))

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by