Finding small vector in big vector
Mostrar comentarios más antiguos
Hey everybody,
I am desperately looking for a function that can find a small vector inside a big vector. Ismember and interesect just wouldn't do it right :(
Let's say I have the two vectors x = [ 7 6 9 7 4 3 7 9 0 7 4 3 2 6 7 0 7 5 ]; y = [ 4 3 2 6 ]
As a result I would like to have ans = [ 11 12 13 14 ]
so only the coordinates of where the values are in the correct sequence. Is there anything that can do that?
Thanks in advance!
Respuesta aceptada
Más respuestas (2)
James Tursa
el 20 de Oct. de 2015
Editada: James Tursa
el 20 de Oct. de 2015
1 voto
I am not on a machine with MATLAB at the moment to test this, but maybe try the strfind(x,y) function to see if it works with doubles (even though the doc doesn't indicate that it does).
3 comentarios
Alexander
el 12 de Sept. de 2022
It does. The numbers are internally casted to char. For compatibility with other programs you can use strfind(char(x),char(y)).
Bruno Luong
el 12 de Sept. de 2022
Editada: Bruno Luong
el 12 de Mayo de 2024
@Alexander Paul "The numbers are internally casted to char."
I don't think so, if it was, the result of the two last commands would be indentiical
d = double('a')+0.1;
a = 'a';
strfind(d,a)
strfind(char(d),char(a))
Alexander
el 12 de Sept. de 2022
You are right. I checked it only with uint8.
Matlab Pro
el 12 de Mayo de 2024
0 votos
Hi
A small improvement where the sub-vector is found more than once...
x = x(:)'; % Make sure data is a Row vector
ix = strfind(x, y);
if length(ix)>1 % Fix cases where y is found more than once
idx = [1:length(y)] + ix(1)-1; % Indexes of 1st occurance
else
idx = strfind(x, y) + [0:length(y)-1];
end
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!