replacing sequences in matrix

조회 수: 2 (최근 30일)
xRobot
xRobot 2019년 11월 22일
댓글: Image Analyst 2019년 11월 23일
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개)

Image Analyst
Image Analyst 2019년 11월 22일
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
xRobot
xRobot 2019년 11월 23일
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.
Image Analyst
Image Analyst 2019년 11월 23일
And what about my first question in my last comment?

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

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by