Finding small vector in big vector

조회 수: 23 (최근 30일)
Lennart
Lennart 2015년 10월 20일
편집: Bruno Luong 2024년 5월 12일
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!

채택된 답변

Image Analyst
Image Analyst 2015년 10월 20일
Try this:
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 ]
out = strfind(x, y) + [0:length(y)-1]
  댓글 수: 2
Lennart
Lennart 2015년 10월 21일
편집: Image Analyst 2015년 10월 21일
Wohoo awesome. Thank you guys so much! That just works perfectly. Actually the strfind itself is just what I need, but the idea of "+[0 : length(y)-1] " is quite needed as well.
Image Analyst
Image Analyst 2015년 10월 21일
You're welcome. strfind(x, y) finds only 11 - the start of the sequence - while adding that other vector gives you [11,12,13,14] which is what you said you wanted.

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

추가 답변 (2개)

James Tursa
James Tursa 2015년 10월 20일
편집: James Tursa 2015년 10월 20일
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
Bruno Luong
Bruno Luong 2022년 9월 12일
편집: Bruno Luong 2024년 5월 12일
@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)
ans = []
strfind(char(d),char(a))
ans = 1
Alexander
Alexander 2022년 9월 12일
You are right. I checked it only with uint8.

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


Matlab Pro
Matlab Pro 2024년 5월 12일
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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by