strfind: how to set a cell for the pattern?

Hi all,
I need to set a cell as pattern for strfind without using a for. Here an example
a={'1','2','3'};
b={'1','2'};
c=strfind(a,b)
c=[1 1 [] ];
thanks
cheers

댓글 수: 5

Sara
Sara 2014년 7월 11일
Why not having a for loop???
pietro
pietro 2014년 7월 11일
I would prefer a faster and more elegant solution but in case it doesn't exist, I'm up for a loop to.
Sara
Sara 2014년 7월 11일
편집: Sara 2014년 7월 11일
Elegant solutions aren't always faster; they are easier to read and save space and coding, I agree on that, but if performance is the issue, you have to test with and without the loop with the profiler (or tic toc). I was just trying to understand what the logic behind the question is.
What about this case:
a={'1','2','3'};
b={'2','1'};
Do you consider that the 1 and the 2 are found/matched, or not? They are not in the same locations , but both are in both arrays.
pietro
pietro 2014년 7월 12일
the output should be {[1],[1],[]}.

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

 채택된 답변

Image Analyst
Image Analyst 2014년 7월 12일
편집: Image Analyst 2014년 7월 12일

0 개 추천

Here is a simple, easy to understand way that will work:
clc; % Clear command window.
% Initialize variables.
a={'1','21','3'};
b={'2','1'};
% Initialize results.
% Let's use a simple numerical array rather than a cell array!
c = zeros(1, length(a));
% Scan each element of "a" for all the elements of "b".
for k = 1 : length(a)
for colb = 1 : length(b)
if ismember(b{colb}, a{k})
c(k) = 1;
break;
end
end
end
% Print out to command window.
c

추가 답변 (4개)

Jos (10584)
Jos (10584) 2014년 7월 11일

0 개 추천

% implicit for with CELLFUN
c = cellfun(@(x) strfind(x,b), a, 'un', 0)

댓글 수: 1

Thanks for your reply. It doesn't work, I get the following error:
Error using cell/strfind (line 33)
If any of the input arguments are cell arrays, the first must be a cell array of
strings and the second must be a character array.
Error in @(x)strfind(x,b)

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

Titus Edelhofer
Titus Edelhofer 2014년 7월 11일

0 개 추천

Hi,
not exactly the same result but similar:
ismember(a, b)
Titus
Chris E.
Chris E. 2014년 7월 11일

0 개 추천

Hello! Well I think this answers your question, it does not have a for loop, however it uses "ismember" rather then 'strfind', but I think the output is the same as what you want.
a={'1','2','3'}
b={'1','2'}
val = ismember(a,b)
val(val == 0)=[]
c = val
Hope that helps!

댓글 수: 1

Unfortunately I cannot use ismember because it is not really equivalent to strfind since it works only when there is an exact match. I'm sorry for not being clearly enough, but it should work also with the following case:
a={'12','2','3'};
b={'1','2'};

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

Jos (10584)
Jos (10584) 2014년 7월 11일

0 개 추천

Then please explain the relationship between a,b,and c. Why is c{2} equal to 1?

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2014년 7월 11일

편집:

2014년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by