how can I exclude NaN when I calculate average?

5 visualizaciones (últimos 30 días)
Sun Wook Han
Sun Wook Han el 2 de Mayo de 2021
Editada: DGM el 2 de Mayo de 2021
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

Respuesta aceptada

DGM
DGM el 2 de Mayo de 2021
Editada: DGM el 2 de Mayo de 2021
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)))

Más respuestas (0)

Categorías

Más información sobre NaNs en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by