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 Center および File ExchangeNaNs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by