say we have a cell array
dictionary= 'abbreviately' 'abbreviation' 'abbreviator' 'abbreviatory' 'abbreviature' 'abby' 'abcoulomb' 'abdal'
I have written a code which returns x = 0 0 0 0 1 0 0
word='saddest'
newguess='e'
x=newguess==word
How do I delete the words from dictionary that are not equivalent for my x value and newguess letter 'e'. So x has a true value on the 5th position (after the 4 false values), which would mean I would like to delete all the words from dictionary if their 5th position letter does not equal 'e'. Thank you

 채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 8일

0 개 추천

newdictionary = C( cellfun(@(C) length(C) >= 5 & C(5)==newguess, dictionary, 'Uniform', 0) );

댓글 수: 5

James Connor
James Connor 2015년 11월 8일
Thanks a lot for that but also, is there a way to write it in a general sense for example if x= 0 0 1 0 1 or x= 0 1 1 0 0 0
newdictionary = C( cellfun(@(C) length(C) == length(x) & all(C(x)==newguess), dictionary, 'Uniform', 0) );
I'm so sorry I didn't define my dictionary correctly it should be
dictionary={'abbreviately';'abbreviation';'abbreviator';'abbreviatory';'abbreviature';'abby';'abcoulomb';'abdal'}
I apologise for the mistake.
Walter Roberson
Walter Roberson 2015년 11월 8일
The code I gave assumed a cell array of strings, so it should be fine with that definition of your dictionary.
James Connor
James Connor 2015년 11월 8일
Oh, I don't know if it's me doing something wrong but I keep getting this.
Index exceeds matrix dimensions.
Error in @(C)length(C)==length(x)&all(C(x)==newguess)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dictionaries에 대해 자세히 알아보기

질문:

2015년 11월 8일

댓글:

2015년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by