how to ignore nan values in corr function?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!