I have two vectors , one called v1 and the other v2 , the size of v1 is 10k , and the other v2's size is 32 , I want to search inside the vector v1 if there's v2 , this means when I find that the vector v1 has the same data as v2 then there would be a high amplitude in the plot .. so Im trying to do do cross corelation between those two vectors in matlab, how can I do that? another question, also Im trying to understand the meaning of cross corelation, so I build one for loop and inside it I did shift indexes by 32 (size of v2 vector) to v2 (v(i:i+32)*v1) -product of sum, and then Im trying to see if the output gies me the required plot to see if there's a high amplituide if v2 found inside v1 .. but the plot is suck and couldn't see anything .. any help how can I do the cross corelation by for loop? thanks alot


Not sure which platform you are using . if it's MATLAB , here is small code snippet which I quickly wrote using MATLAB's inbuilt "strfind" function

A = [12  534 5 23 44 66  23 68  86854  7  36 457 68 23 46 87 78 588 86 8 44 66  23 68  86854 87 9 905  0 -213 34];
B = [44 66  23 68  86854]

strfind(A,B)    % returns 5 and 21 (starting index of Array B)

You can always use cross-correlation to find the similarity between A and B

Please go through this post . It has detailed explanation with examples.



you can alternatively use xcorr function too

For the same example vectors A, B .. you can do something like this

xCorrResult = xcorr(B,A);

enter image description here

You can see two peaks at index 11 and 27 which indicates B vector lies in A at indexes 5 (11 - length(B) -1) and 21 (27 - length(B) - 1)

  • $\begingroup$ Hi @Saira , Im using matlab platform , Im trying to do like this: I have two vectors one called v1 and the other v2 with difference size, trying to find out if v2 is found in v1 and once there's v2 inside v1 then at the plot of cross corelation there would be a high amplitude , this means when I do cross corelation v2 with v1 , and I plot the cross corelation of those two vectors, then the plot would be like there's a high amplitude once v1 figured in v2 and otherwise would be zero .. $\endgroup$ – Ryan Jun 7 '20 at 18:09
  • $\begingroup$ like something _______|_____|_____| , here for instance v1 appeared in v2 three times because there's three highs with high amplitudes in the plot of cross corelation .. so I want to do something like that any help please? $\endgroup$ – Ryan Jun 7 '20 at 18:12
  • $\begingroup$ If you go through the "mathworks" link , it gives you a very best explanation of cross-correlation with an example . I have edited my answer using xcorr function. $\endgroup$ – Saira Jun 7 '20 at 18:29
  • $\begingroup$ Hi Saira, thanks alot! but pelase could you explain to me the same concept of cross corelation by loop for with shifted (as what I explained in my post- the shift was by 32 because the other vector is with size 32 ) ? what Im trying to do the same what cross corelation does but by convolution/for loop with correct shift in the vector $\endgroup$ – Ryan Jun 7 '20 at 18:43
  • $\begingroup$ I would thank you alot if you explain that by for loop with vector shifted as convolution concept .. like v1(i:i+32) * v2 , v2 is size 32 .. so the shift must be 32 as I did in v1 .. and the product between those two vectors for every i would give me in the total matrix/vector that if I did plot to it then it would give me the same cross corelation plot ..! I understand that inner product with shifted would give me the same as cross corelation but Im trying to implement that by for loop (in other word to implement cross corelation by for loop to understand it correctly ) $\endgroup$ – Ryan Jun 7 '20 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.