replacing sequences in matrix

Hello, in the code below I am attempting to do the following:
yourarray = ['hood'; 'hope'; 'make';];
guess = input('Enter a four letter word: ');
yourarray(ismember(yourarray, 'guess', 'rows'), :) = [];
disp(yourarray);
  1. Take in user input for a four letter word
  2. Remove the four letter input from the matrix then display the resulting matrix with the user input removed
I have had no luck. I am trying make it so that I can remove any sequence letter the user chooses for instance, instead of one of the three words in the matrix he may choose "mope". How can I better accomplish this? Any advise is greatly appreciated. I have tried other methods but landed on this one as being what I think will be the closest possible solution. Thanks for any help!

回答(1 个)

Try this:
yourarray = {'hood'; 'hope'; 'make'}; % Cell array.
guess = input('Enter a four letter word: ', 's'); % Use 's' option.
index = ismember(yourarray, guess, 'rows')
if all(index == 0)
% All zeros mean it was not found.
message = sprintf('%s was not found in the list.', guess);
uiwait(warndlg(message));
else
% If any element of index is 1 then it was found at that row.
row = find(index)
yourarray(row, :) = [];
celldisp(yourarray);
end

4 个评论

It worked sort of but what I am really getting at(my initial post wasn't very clear) is this:
I have a block of type char like this,
ooda
haaa
aaaa
The letters are arbitrary for now, also the actual block has no spaces in my program. Say the user looks and finds the word "hood" in the block. I would like to remove his choice of "hood" from the block and then put the resulting block into a variable and display like:
a
aaa
aaaa
With his selection removed. Hopefully this is clearer. You can disregard the string array as that will be a seperate function in my program called "verify()" to check to see if the user's selection is actually in my large string array of four letter words.
Did you try strfind(), strrep(), or contains()? So the letters don't have to be contiguous? What if the 4 letters occur in hundreds of locations? Replace the letter wherever it appears with a space with strrep()????
No the letters can be found and combined from anywhere in the block. That is the basis of the game to find hidden four letter words in a scrambled block of letters.
And what about my first question in my last comment?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

标签

提问:

2019-11-22

Community Treasure Hunt

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

Start Hunting!

Translated by