NaN result on xcorr?
8 views (last 30 days)
Show older comments
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).
0 Comments
Accepted Answer
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
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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!