how can I exclude NaN when I calculate average?

조회 수: 5 (최근 30일)
Sun Wook Han
Sun Wook Han 2021년 5월 2일
편집: DGM 2021년 5월 2일
how can I exclude NaN when I calculate average?
For example,
A = [1 3 5 NaN 9]
How can I calculate average of A? The answer should be (1+3+5+9)/4

채택된 답변

DGM
DGM 2021년 5월 2일
편집: DGM 2021년 5월 2일
If you're using a reasonably new version:
A = [1 3 5 NaN 9]
B = mean(A(:),'omitnan')
If you need it to work in older versions:
B = mean(A(~isnan(A)))

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