How to exclude empty values from an array during statitical calculations?

5 vues (au cours des 30 derniers jours)
Emerson De Souza
Emerson De Souza le 28 Août 2011
Commenté : Jon le 23 Oct 2018
I have an array build of numbers and empty values NaN and I want to
calculate statistical quantities such as median, mean, etc... but not
including the NaN in the calculation.
For example, the median of the following array
NaN
NaN
1.14353916977288
1.13195167986180
NaN
NaN
1.27935109630269
NaN
1.15339038040937
NaN
equals NaN because most of the array elements are NaN. But my
intention was to calculate the median ONLY of the elements with
values (in this particular case, the median of only 4 elements).
I wonder if someone could tell me how to exclude NaN from the
calculation without need to change the Matrix.
Thank you
Emerson

Réponse acceptée

Oleg Komarov
Oleg Komarov le 28 Août 2011
Look at the function isnan.
A = [NaN; NaN; 1.14353917; 1.13195168; NaN; NaN; 1.279351096; NaN; 1.15339038; NaN]
median(A(~isnan(A)))
Also, if you have the statistics toolbox look at nanmedian and similar.
  1 commentaire
Jon
Jon le 23 Oct 2018
An alternative is to use the optional nanflag argument to median, as indicated in the documentation for median "M = median(___,nanflag) optionally specifies whether to include or omit NaN values in the median calculation for any of the previous syntaxes. For example, median(A,'omitnan') ignores all NaN values in A."

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur NaNs dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by