Find whether a vector occurs exactly in another vector

32 ビュー (過去 30 日間)
Mark
Mark 2015 年 1 月 29 日
コメント済み: Guillaume 2015 年 1 月 29 日
Hi,
I try to compare two vectors (waveforms from wav files). One is the original sound file and the other is a manipulated sound file. I try to find the indices in the original sound file that match with the manipulated sound file. In the end, I would like to plot the original sound file and highlight the parts that were manipulated.
Loading the files into Matlab is no problem, but finding a way to compare/match the files is difficult. I tried the "ismember" function, but I think this function will try to find a match for each datapoint whereas I need something that matches a complete vector with original sound files.
Any suggestion on how to find whether a small vector occurs exactly in another longer vector?
Best, Mark

採用された回答

Guillaume
Guillaume 2015 年 1 月 29 日
Surprisingly enough, strfind works on numerical arrays as well and does exactly what you need:
x = [4 3 1 7 36 4 2 3 1 2 58 8 4 6 99 4 3 2 1 5 7 6 4 1 8 4 6 99 4 3 2 1 3 9 7 7 4 3 5];
y = [8 4 6 99 4 3 2 1]; %occurs at position 12 and 25 in x
pos = strfind(x, y)
  2 件のコメント
Cedric
Cedric 2015 年 1 月 29 日
+1 This has been discussed a few times by Loren, for example here.
Guillaume
Guillaume 2015 年 1 月 29 日
I think that's indeed on Loren's blog that I first came across it.

サインインしてコメントする。

その他の回答 (2 件)

Eric G.
Eric G. 2015 年 1 月 29 日
This may be a brute force way but MATLAB should be able to handle it with no problem.
Just scan the shorter vector across the longer one and compare for equality.
x= [4 3 1 7 36 4 2 3 1 2 58 8 4 6 99 4 3 2 1 5 7 6 4 1 3 9 7 7 4 3 5];
y = [8 4 6 99 4 3 2 1];
ny = length(y);
for k = 1:(length(x)-ny+1)
check = x(k:k+ny-1)== y;
if sum(check) == ny
fprintf('Found a match at k = %2.0i \n',k)
end
end

Andrei Bobrov
Andrei Bobrov 2015 年 1 月 29 日
編集済み: Andrei Bobrov 2015 年 1 月 29 日
other variant
n = numel(y);
pos = find(all(bsxfun(@eq,y(:),hankel(x(1:n),x(n:end)))));

カテゴリ

Help Center および File ExchangeApplication Deployment についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by