フィルターのクリア

Nested mean function with omitnan

10 ビュー (過去 30 日間)
Stefan Th.
Stefan Th. 2023 年 3 月 16 日
コメント済み: Stefan Th. 2023 年 3 月 16 日
Not sure if this behavior is intentional. When I have an array, let's say:
A=[1,NaN;3,4];
And I want to calculate the mean over the second dimension first, then the first one:
m=mean(A,[2,1],'omitnan');
The result is 2.66, which is clearly just (1+3+4)/3, and so it's the same as:
m=mean(A,'all','omitnan');
I'm aware that this is exactly what it says in the documentation, but it's still unexpected. What I would have expected would be the same as:
m=mean(mean(A,2,'omitnan'),1,'omitnan');
Which in this case gives the (imho correct) result of 2.25, so (1+(3+4)/2)/2
Am I missing something?
  2 件のコメント
KSSV
KSSV 2023 年 3 月 16 日
You think?
(1+(3+4)/2)/2 == (1+3+4)/3
ans = logical
0
Stefan Th.
Stefan Th. 2023 年 3 月 16 日
That's...my point

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

採用された回答

Jan
Jan 2023 年 3 月 16 日
The problem is here: "What I would have expected would be the same as". This is not the way mean('omitnan') is working. So if you expect something else, you have to write the code to do it as you want it.
If it is explained in the documentation, there is no reason to expect something else. The behavior you expect would not match my intuition for [NaN, 1; NaN, 2].
  1 件のコメント
Stefan Th.
Stefan Th. 2023 年 3 月 16 日
Sure, it's in the documentation, so that's fine, I just think 'omitnan' could be misinterpreted in this case.
[NaN, 1; NaN, 2] would always lead to the same result, no matter in what order you calculate the mean or if you just take the mean of all values all together, but it's different in the case I mentioned.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by