Searching word list for key letters

조회 수: 1 (최근 30일)
user86753
user86753 2022년 1월 24일
댓글: Walter Roberson 2022년 1월 24일
wordList = {'apples'
'orange'
'banana'};
ContainList = 'shu';
NotContainList = 'br';
I'd like to be able to find all words that contain the letters S,H or U and do not have the letters B or R. I'm using 'contains' for each letter but is there a way to loop through the ContainList/NotContainList for each word?

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 24일
편집: Walter Roberson 2022년 1월 24일
wordList = {'apples'
'orange'
'banana'};
ContainList = 'shu';
NotContainList = 'br';
mask1 = contains(wordList, num2cell(ContainList))
mask1 = 3×1 logical array
1 0 0
mask2 = contains(wordList, num2cell(NotContainList))
mask2 = 3×1 logical array
0 1 1
filtered_words = wordList(mask1 & ~mask2)
filtered_words = 1×1 cell array
{'apples'}
%another approach
mask3 = ~cellfun(@isempty, regexp(wordList, "[" + ContainList + "]"))
mask3 = 3×1 logical array
1 0 0
mask4 = ~cellfun(@isempty, regexp(wordList, "[" + NotContainList + "]"))
mask4 = 3×1 logical array
0 1 1
filtered_words2 = wordList(mask3 & ~mask4)
filtered_words2 = 1×1 cell array
{'apples'}
%another approach
pattern = "^[^" + NotContainList + ContainList + "]*[" + ContainList + "][^" + NotContainList + "]*$"
pattern = "^[^brshu]*[shu][^br]*$"
mask5 = ~cellfun(@isempty, regexp(wordList, pattern))
mask5 = 3×1 logical array
1 0 0
filtered_words3 = wordList(mask5)
filtered_words3 = 1×1 cell array
{'apples'}
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 1월 24일
In some cases you might want to work iteratively
wordList = {'apples'
'orange'
'banana'};
ContainList = 'shu';
NotContainList = 'br';
words_containing = wordList(contains(wordList, num2cell(ContainList)));
filtered_words = words_containing(~contains(wordList, num2cell(NotContainList)));
In situations where you are doing stepwise refinement, it is most efficient to start from the steps that are expected to make the most difference, each step discarding as much as practical, so that each step is searching less and less information.

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

추가 답변 (1개)

David Hill
David Hill 2022년 1월 24일
wordList = {'apples','orange','banana','show','unit'};
ContainList = 'shu';
NotContainList = 'br';
r=wordList(contains(wordList,num2cell(ContainList))&~contains(wordList,num2cell(NotContainList)));

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by