taking second mode from strings

2 次查看(过去 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