What is the difference between mean(A,'omitnan') and nanmean(A) ?

201 ビュー (過去 30 日間)
LeChat
LeChat 2018 年 3 月 19 日
編集済み: Andrew Frane 2024 年 12 月 4 日
Hi, I have a vector A (or could be a matrix) which contains finite values as well as NaNs. I want to compute the mean value of its non-NaN elements. What is the difference between mean(A,'omitnan') and nanmean(A) ? Thank you!

採用された回答

Stephen23
Stephen23 2018 年 3 月 19 日
There is no practical difference for your vector.
Personally I would use mean, because nanmean is part of the Statistics Toolbox, so using it makes your code less portable.
  3 件のコメント
Nathan Williamson
Nathan Williamson 2020 年 5 月 2 日
On a related note, in my code I found median(A,'omitnan') to run significantly faster than nanmedian(A). Not sure why. In the same code, mean(A,'omitnan') and nanmean(A) have a similar runtime.
Andrew Frane
Andrew Frane 2024 年 12 月 4 日
編集済み: Andrew Frane 2024 年 12 月 4 日
Looking at the code for the most recent (2020) version of nanmean, it's just a wrapper-function that executes the mean function with 'omitnan'. So it should perform nearly as efficiently.
But looking at the code for the most recent (2020) version of nanmedian, for some reason it doesn't just execute the median function with 'omitnan'. Instead, it just uses the prctile function to calculate the 50th percentile, which isn't a very computationally efficient way to calculate the median.
Incidentally, if A is a vector, (or if you want a single grand mean of a matrix or higher-dimensional array), you could also do something like the following, which avoids both the back-compatibility problem with 'omitnan' and the toolbox-dependence problem with nanmean and has similar efficiency:
mean( A(~isnan(A)) )

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 3 月 19 日
nanmean() existed first, as part of the Statistics Toolbox.
Later 'omitnan' was added as an option to the regular mean() as part of basic MATLAB. nanmean() should now be used mostly for backwards compatibility.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by