Finding small vector in big vector
24 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
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 件のコメント
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
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
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)
strfind(char(d),char(a))
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!