How to find similar indices in two vector

1 view (last 30 days)
Shekhar Vats
Shekhar Vats on 31 Jan 2020
Answered: edward holt on 31 Jan 2020
I have a data set where one vector, say 'A' is a subset of vector 'B'. For example vector B = [x,y ,z, A,c , d]
Is there a way i can find the indices of A in B ?
Note: Please keep in mind that vector A is of size 13000 X 1 while vector B is of size 55000 X 1
I have tried xcorr, findsignal and strfind and it's not giving intended results

Answers (1)

edward holt
edward holt on 31 Jan 2020
There is probably a better (faster) way than this. But it seems to work.
A = randi(1,10000,1);
B = zeros(60000,1);
%inserting A somehwere into B
B(45213:55212) = A;
for i = 1:length(B) - length(A)
if nnz(B(i:i+length(A)-1,:) == A) == length(A)
index = i;
end
end
index
%returns starting point of A within B.

Community Treasure Hunt

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

Start Hunting!