Find correlation between two matrices with varying NaN coordinates
12 views (last 30 days)
Show older comments
Hello. I have global two matrices, A and B
They are both the same dimensions, but A has NaN's in some places that B does not. When I use corrcoef, it returns NaN's, even though the values within A and B are not identical. Is it returning NaN because the varying coordinates of NaN values within each matrix?
Does anyone know what I'm doing wrong in this case?
Thanks,
Melissa
0 Comments
Answers (1)
Chad Greene
on 18 May 2015
Indeed,
A = [1 2 3 4 5 6 7];
B = [1 2 2 4 5 6 NaN];
corrcoef(A,B)
ans =
1 NaN
NaN NaN
But if you consider only non-NaN data:
% indices of non-nan data:
realz = isfinite(A) & isfinite(B);
% correlation coefficient of non-nan data:
corrcoef(A(realz),B(realz))
ans =
1.0000 0.9786
0.9786 1.0000
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!