strfind: how to set a cell for the pattern?
Mostrar comentarios más antiguos
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 comentarios
Sara
el 11 de Jul. de 2014
Why not having a for loop???
pietro
el 11 de Jul. de 2014
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.
Image Analyst
el 11 de Jul. de 2014
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
el 12 de Jul. de 2014
Respuesta aceptada
Más respuestas (4)
Jos (10584)
el 11 de Jul. de 2014
% implicit for with CELLFUN
c = cellfun(@(x) strfind(x,b), a, 'un', 0)
1 comentario
pietro
el 11 de Jul. de 2014
Titus Edelhofer
el 11 de Jul. de 2014
Hi,
not exactly the same result but similar:
ismember(a, b)
Titus
Chris E.
el 11 de Jul. de 2014
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 comentario
pietro
el 11 de Jul. de 2014
Jos (10584)
el 11 de Jul. de 2014
0 votos
Then please explain the relationship between a,b,and c. Why is c{2} equal to 1?
1 comentario
pietro
el 11 de Jul. de 2014
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!