how to ignore nan values in corr function?
3 views (last 30 days)
Show older comments
trailokya
on 17 Dec 2014
Answered: Roger Stafford
on 17 Dec 2014
sir, i have a three dimensional matrix say A(129*135*33). I have to find out the correlation coefficient along the third dimension with B=[1981:2013]. i am using the following code
m1=squeeze(A(i,j,:));
m2=[1981:2013]';
p=[m1 m2];
q=p(isfinite(p(:, 2)), :); %to remove the rows with nan values
CC(i,j)=corr(q(:,1), q(:,2);
due to some grid which have only nan values the program is not working and giving the following message
Error using corr (line 87) Requires a data matrix X.
please help how to solve it?
0 Comments
Accepted Answer
Roger Stafford
on 17 Dec 2014
Clearly there is no sensible way to compute a correlation value for two empty vectors. I would suggest that for such cases you place a NaN in the CC matrix at the corresponding i,j pair:
q = ...
if numel(q) > 0
CC(i,j) = corr(...)
else
CC(i,j) = NaN;
end
0 Comments
More Answers (0)
See Also
Categories
Find more on NaNs 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!