comparing to string array
이전 댓글 표시
Hello, I have a large string array with many four letter words called "words". I also have a matrix of char that is has been scrambled called "scrambled"(it is a block of letters). I am attempting to sort all possible combinations of the char matrix and compare them to my list to see if there any possible combinations of four letters that match a word in my list. I have tried a couple of things that did not work. I am new to matlab so I am having trouble with the process. Below are examples of the variables I am working with. Any advice is greatly appreciated. Thanks in advance!
words = ["make" "hope" "list" "home"]
scrambled =
kame
stit
abnaa
댓글 수: 7
the cyclist
2019년 11월 22일
Did you intend scrambled to be a 3x4 character array, like this?
scrambled = ['kame';
'stit';
'abna'];
Can letters be chosen from anywhere in the array to be "unscrambled", or only from a single row?
Walter Roberson
2019년 11월 22일
abnaa is not 4 letters?
xRobot
2019년 11월 22일
xRobot
2019년 11월 22일
Walter Roberson
2019년 11월 23일
Your algorithm is not efficient.
For any given word,
if all(ismember(thisword, available_letters))
then the word can be formed and you can quit checking at that point.
However you do need to modify this algorithm to account for multiple copies of the same letter being required, such as being able to figure out that you cannot form 'boss' if you only have one available s.
xRobot
2019년 11월 23일
Walter Roberson
2019년 11월 23일
can_form_a_word = false;
for thisword = words(:).'
if all(ismember(thisword{1}, letters_remaining_in_block))
can_form_a_word = true;
break;
end
end
if ~can_form_a_word
disp('No more word soup for you!')
end
답변 (1개)
Walter Roberson
2019년 11월 22일
ScS = string(scrambled);
[present, index] = ismember(ScS, words) ;
present will be a logical vector. Each location that is true will have a corresponding value in index that will reflect the position inside words that scrambled came from. For example if present(2) is true and index(2) is 7 then ScS(2) is words(7)
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!