curious behavior of geomean

3 ビュー (過去 30 日間)
Kristopher Dawsey
Kristopher Dawsey 2020 年 11 月 9 日
回答済み: Steven Lord 2020 年 11 月 10 日
I'm using matlab 2017a.
I've been having some trouble getting the expected output from geomean.
In debugging this, per the geomean documentation here https://www.mathworks.com/help/stats/geomean.html
i tried to run the following code
x = 1:10;
x(3) = nan; % Replace the third element of x with a NaN value
n = geomean(x,'omitnan')
And the expected response as mentioned in the documentation is n = 4.7408
In contrast, i get back
Error using size
Dimension argument must be a positive integer scalar within indexing range.
Error in geomean (line 26)
n = size(x,dim);
This seems like a pretty foundational problem if even this simple code example from the documentation is generating an error.
Any thoughts as to what might be going on here?

採用された回答

John D'Errico
John D'Errico 2020 年 11 月 9 日
編集済み: John D'Errico 2020 年 11 月 9 日
As is always the case, consider what MATLAB release you are using.
x = 1:10;
x(3) = nan; % Replace the third element of x with a NaN value
n = geomean(x,'omitnan')
n = 4.7408
As you can see, when run using a current release, it works fine. The documentation you reference is for the CURRENT release (which would be R2020b), but you say you are using R2017a.
Things change. Options get added in newer releases. You can't always assume backwards compatibility for all new releases.
  1 件のコメント
Kristopher Dawsey
Kristopher Dawsey 2020 年 11 月 10 日
Thanks a lot for the response, John. Yes i think you're exactly right. The backward compatible version i suppose would just be the below. Sorry I should have thought of that as a likely explanation. Thanks again.
>> n = geomean(x(~isnan(x)))
n =
4.7408

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 11 月 10 日
According to the Release Notes the ability to specify 'omitnan' to omit NaN values from the geometric mean calculations in geomean was introduced in release R2019a.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by