Efficient method of searching cell array for multiple, partial (non exact) strings

조회 수: 8 (최근 30일)
Hello, internet!
I'm looking for the most efficient way to look through a cell array to match partial strings. I know I can bound things in a loop, but there has to be a more efficient method of doing this.
Given the following:
MasterList = {'dog' 'house' 'hotdog' 'cat' 'house music' 'banana' 'that actor from the 80s'};
SearchFor = {'dog' 'house' 'actor'};
I would like a search algorithm to return:
LogicalArrayX = 1 1 1 0 1 0 1
I know that partialLogicalArrayX = strfind(MasterList,SearchFor{i}) will return the character indices of MasterList when searching for SearchFor{i}, but I'd like it to return a logical matrix and be able to batch-process SearchFor instead of having to loop around the i variable.
Thanks!

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 9월 1일
편집: Azzi Abdelmalek 2016년 9월 1일
edit
MasterList = {'dog' 'house' 'hotdog' 'cat' 'house music' 'banana' 'that actor from the 80s'};
SearchFor = {'dog' 'house' 'actor'};
LogicalArrayX=logical(~cellfun(@isempty,regexp(MasterList,'dog|house|actor','once')))
  댓글 수: 2
Matt C
Matt C 2016년 9월 1일
Thanks, Azzi. Is there a way to extend this so that you aren't manually entering 'dog|house|actor'? This was my simplified problem - my real problem has a MasterList that is tens of 1000s of items long, and my SearchFor uses something that is 1000s of items long. Computational speed is also critical for my application, and I have seen on some threads that regexp operates fairly slowly.
Azzi Abdelmalek
Azzi Abdelmalek 2016년 9월 1일
편집: Azzi Abdelmalek 2016년 9월 1일
MasterList = {'dog' 'house' 'hotdog' 'cat' 'house music' 'banana' 'that actor from the 80s'};
SearchFor = {'dog' 'house' 'actor'};
patern= strjoin(SearchFor,'|')
LogicalArrayX=logical(~cellfun(@isempty,regexp(MasterList,patern,'once')))

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

카테고리

Help CenterFile Exchange에서 Clocks and Timers에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by