how can I exclude NaN when I calculate average?

2 次查看(过去 30 天)
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