taking second mode from strings
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!