how to ignore nan values in corr function?

8 views (last 30 days)
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?

Accepted Answer

Roger Stafford
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

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!