NaN result on xcorr?

8 views (last 30 days)
Guy
Guy on 25 Sep 2012
Hello everyone. I am trying to use both xcorr(var1,var2,0,'coeff' and corrcoef in order to get a correlation coefficient of two vectors (I want to see how "similar" they are).
The thing is, sometimes I need to insert a vector of zeros into that calculation and the I get NaN as a result.
It is very important for me to be able to correlate between a vector of zeros and a vector of really small numbers ( both of same length, of course), and get a relatively good correlation between the two (relative to a vector of ones with a vector of small numbers, for example).

Accepted Answer

Wayne King
Wayne King on 25 Sep 2012
Edited: Wayne King on 25 Sep 2012
If you have a a vector of zeros, then using the 'coeff' option will give you NaNs because you are normalizing by the product of the input norms. The norm of the zero vector is 0 (it is the only vector with zero norm by the definition of a norm).
So you have a vector of 0/0
You can use the 'unbiased', 'biased', or 'none' options (the last one does not need to be specified), but you will get a vector of zeros of course.
If you cross correlate any vector with the zero vector, the answer must be a vector of zeros. How can it be anything else? The cross correlation involves an element-by-element product and sum. All your products must be zero if one of the inputs is the zero vector.
  3 Comments
Wayne King
Wayne King on 25 Sep 2012
If these are just points on a line, what not just orthogonally project each line onto the reference line?; that will give you the best approximation in the minimum squared error (2-norm) sense
Guy
Guy on 25 Sep 2012
Can you detail a little further? How do I do that?
Also forgot to mention, I figured the problem with the correlation might be a devision by zero, so I transferred zero vectors into 'eps' vectors, meaning that I gave them some insignificant value that will not have any affect on the desirable result. The outcome then was -1, which I could not understand because I correlated it with a vector of small values as well. Anyway, this did not work for me but maybe your idea will?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!