How do you take the nanmean of a 3D matrix?

3 ビュー (過去 30 日間)
HollyA
HollyA 2018 年 1 月 26 日
コメント済み: Walter Roberson 2018 年 1 月 26 日
I have ten matrices that I want to average, but some have NaNs in them. They are of the dimension 64x128x12. How do I get the mean of the ten without including NaN in the calculation and still maintaining the dimensions of 64x128x12?
I have tried nanmean and nanmean2 to no luck.
Thank you!
  4 件のコメント
HollyA
HollyA 2018 年 1 月 26 日
A full mean of the ten, similar to if I just did a mean mannually (add them up and divide by 10) but where some have NaN, to ignore that and continue to calculate the mean of the others as a number?
I hope this makes sense?
Adam
Adam 2018 年 1 月 26 日
Well, 'mean of the ten' could still mean take the mean value of all data in all 10 matrices, but from your clarification I assume you mean the end result should be 64x128x12 too.
Walters answer of stacking them into a 4-d 64x128x12x10 matrix and taking the nanmean (or simply 'mean' with an 'omitnan' flag which is the same thing anyway) seems fine to me...

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

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 1 月 26 日
means_of_10 = nanmean( cat(4, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10), 4);
  2 件のコメント
HollyA
HollyA 2018 年 1 月 26 日
I tried this but it seemed to give me a very odd value and not quite work. Thank you for your answer though!
Walter Roberson
Walter Roberson 2018 年 1 月 26 日
To confirm: if there was (say) 2 nan out of 10, you would want the others totaled and divide by (10-2) = 8? Or would you want to divide by 10?
You can code more explicitly as:
temp = cat(4, M1, M2, M3, M4, M5, M6, M7, M8, M9, M10);
nans = isnan(temp);
temp(nans) = 0;
means_of_10 = sum(temp, 4) ./ (size(temp,4) - sum(nans,4));
If this does not give you the results you expect then you should check your values more closely.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by