Find correlation between two matrices with varying NaN coordinates
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Chad Greene
2015-5-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!