0

I have problem with using 'corr' function in MATLAB,

a =

     1     4     3     2
     2     3     3     2
     3     2     3     2
     4     1     3     2

>> corr(a)

ans =

     1    -1   NaN   NaN
    -1     1   NaN   NaN
   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN

When I calculate manually, the Missing Value (NaN) is because the denominator is ZERO (0). Although, we can be see that similarity of column 3 and 4 is ONE (+1).

Anyone know how to enhance or replace the missing value?

Thank's Before.

zellus
  • 9,617
  • 5
  • 39
  • 56
user555928
  • 1
  • 1
  • 1

2 Answers2

2

What do you expect, correlation is a measure of linear dependence between two variables, computed as the covariance (how much the variables change together) normalized by the standard deviations.

Thus it makes no sense if one variable is constant (you get zero divided by zero which is undefined and reported as NaN)...

Amro
  • 123,847
  • 25
  • 243
  • 454
  • so, it means when the variable is constant, the correlation cannot be calculated. Is it possible to using exception? btw, thanks for answering though, this is helping me a lot. – user555928 Dec 28 '10 at 17:15
0

As Amro said, corr is reporting the correct answer, which is undefined. If you want to handle the undefineds a special way, such as setting to 1, you can do this:

a(isnan(a)) = 1;

But it sounds like you have some deeper issues with your data - not enough observations? Why not remove identical series?

Rich C
  • 3,164
  • 6
  • 26
  • 37