how to ignore nan values in corr function?

조회 수: 3 (최근 30일)
trailokya
trailokya 2014년 12월 17일
답변: Roger Stafford 2014년 12월 17일
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?

채택된 답변

Roger Stafford
Roger Stafford 2014년 12월 17일
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개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by