how can I exclude NaN when I calculate average?

1 vue (au cours des 30 derniers jours)
Sun Wook Han
Sun Wook Han le 2 Mai 2021
Modifié(e) : DGM le 2 Mai 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

Réponse acceptée

DGM
DGM le 2 Mai 2021
Modifié(e) : DGM le 2 Mai 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)))

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by