Cumtrapz for matrix including NaN values

조회 수: 22 (최근 30일)
Anna S
Anna S 2019년 4월 15일
댓글: Anna S 2019년 4월 15일
I want to integrate a data field which sometimes also contains Nan values. The integration with cumtrapz then always is NaN as well, of course.
Is there a function (as for example nanmean instead of mean) which ignores NaN?
SALT = [ NaN NaN NaN NaN
NaN NaN NaN NaN
31.7313 NaN NaN NaN
31.9400 31.6944 NaN NaN
32.1753 31.9462 NaN NaN
32.2596 32.0371 31.7931 NaN
32.3248 32.2337 32.0498 NaN]
Q= cumtrapz(SALT)

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2019년 4월 15일
Well, I think that in addition to calculating a cummulative integration you also should/need to extract the limits over which you have non-nan values, something like this:
for i2 = 1:size(SALT,2)
i1 = find(isfinite(SALT(:,i2)));
if ~isempty(i1)
I_OK(:,i2) = [i1(1) i1(end)];
CTS{i2} = cumtrapz(SALT(i1,i2));
end
end
In addition you should only use cumtrapz over contiguous regions with finite values, so my example works for the data you gave. You might(should) make sure that the indices i1 are without gaps/or take care of the cases where there are gaps.
HTH
  댓글 수: 1
Anna S
Anna S 2019년 4월 15일
Thanks, that's what I was searching for!

댓글을 달려면 로그인하십시오.

추가 답변 (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