Azzera filtri
Azzera filtri

taking second mode from strings

3 visualizzazioni (ultimi 30 giorni)
Max
Max il 11 Nov 2015
Commentato: Thorsten il 11 Nov 2015
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

Risposte (1)

Thorsten
Thorsten il 11 Nov 2015
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 Commenti
Max
Max il 11 Nov 2015
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 il 11 Nov 2015
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)))

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by