# corr returns NaN

179 views (last 30 days)
D on 7 Jul 2011
Answered: Bio_Ing_Sapienza on 24 Feb 2019
I'm calculating the correlation coefficient between two vectors, using corr. It returns NaN, and I'm curious to know why that is the case.
c and d are the vectors.
>> [c d]
ans =
1 1
2 1
2 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
2 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1

Sean de Wolski on 7 Jul 2011
doc corr
and you'll see it's not what you want. Are you looking for :
doc corrcoef %?

D on 7 Jul 2011
Yes, thank you. But I still get
corrcoef(c,d)
ans =
1 NaN
NaN NaN
This seems to point at something in d as being an issue.
Sean de Wolski on 7 Jul 2011
It's because there's no variance/ standard deviation in the second column and thus in the correlation coefficient calculation when you divide by std or var (however it's implemented) you're in turn dividing zero by zero which yield nan.
D on 7 Jul 2011
Thank you. I missed that.

Bio_Ing_Sapienza on 24 Feb 2019
Maybe you have to ceck for "Inf" components in your array. Sometimes I was searching from Nan even if I had already checked them out. But there are other kind of Matlab numeric representation for results came from division that yields to too large numbers to be represented with floating number. So check also for these ones!