필터 지우기
필터 지우기

Averaging sections of a vector

조회 수: 4 (최근 30일)
Thishan Dharshana Karandana Gamalathge
댓글: Andrei Bobrov 2017년 7월 28일
A=[1 3 -999 -999 -999 -999 4 9 8 -999 -999 -999]; Here I want to get averages of three elements by ignoring -999. But in the final answer, if there were cases with three -999 in a given sector, it needs to be replaced by NaN. For an example, final answer of the above should B=[2 NaN 7 NaN]
Thanks.

채택된 답변

Steven Lord
Steven Lord 2017년 7월 28일
% Sample data
A=[1 3 -999 -999 -999 -999 4 9 8 -999 -999 -999];
% Replace the 'missing' -999 values with NaN
A(ismissing(A, -999)) = NaN;
% If you have a release that doesn't include ismissing, use ==
A(A == -999) = NaN;
% Take the mean, omitting NaN unless all the elements whose mean
% are being computed are NaN
M = mean(reshape(A, 3, []), 'omitnan')
  댓글 수: 1
Andrei Bobrov
Andrei Bobrov 2017년 7월 28일
% Sample data
>> A=[1 3 -999 3 -999 -999 4 9 8 9 -999 -999];
% Replace the 'missing' -999 values with NaN
A(ismissing(A, -999)) = NaN;
% If you have a release that doesn't include ismissing, use ==
A(A == -999) = NaN;
% Take the mean, omitting NaN unless all the elements whose mean
% are being computed are NaN
M = mean(reshape(A, 3, []), 'omitnan')
M =
2 3 7 9
>>

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 7월 28일
z = A;
z(z == -999) = nan;
t = ~isnan(z);
B = accumarray(cumsum(diff([~t(1);t(:)]) ~= 0),z,[],@mean);

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by