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 CenterFile Exchange에서 Application Deployment에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by