Undefined function 'geomean' for input arguments of type 'double' in Matlab 2020
Show older comments
Dear all,
I have a simple code containing "geomean" for a data with nan values. I tried this code:
geomean(X,'omitnan')
in Matlab 2022 and it runs well. But, when I tried in Matlab 2021, I got this message:
Undefined function 'geomean' for input arguments of type 'double'.
I also tried a classical way:
geomean(X(~isnan(X)));
And it gives me the same problem.
Any suggestion regarding this error?
Accepted Answer
More Answers (2)
An alternative is:
gm = @(x) prod(x)^(1/numel(x));
x = rand(1,100);
y1 = geomean(x)
y2 = gm(x)
.
11 Comments
An approach that should be more resilant for larger values:
format long g
gmL = @(x) exp(mean(log(x)));
gm = @(x) prod(x)^(1/numel(x));
x = 10000*rand(1,100);
y1 = geomean(x)
y2 = gm(x)
y3 = gmL(x)
Star Strider
on 4 Mar 2023
@Walter Roberson — Noted! Thank you!
Walter Roberson
on 4 Mar 2023
My formula does not work if you have negative values. On the other hand, it is often said that the geometric mean is not defined when there are negative values. geomean() refuses to calculate with negative values.
Star Strider
on 4 Mar 2023
I believe that is part of its general definition, considering its geometric origins: Geometric mean (Wikipedia). Triangles cannot have negative sides.
Adi Purwandana
on 5 Mar 2023
Star Strider
on 5 Mar 2023
What numbers?
Testing with numbers in the stated range, the functions seem to work.
format long g
gmL = @(x) exp(mean(log(x)));
gm = @(x) prod(x)^(1/numel(x));
x = exp(-rand(1,10)*25);
[min(x), max(x)]
y1 = geomean(x)
y2 = gm(x)
y3 = gmL(x)
Adi Purwandana
on 5 Mar 2023
Adi Purwandana
on 5 Mar 2023
Star Strider
on 5 Mar 2023
I do not understand what you are doing.
Numeric functions will not usually work on non-numeric (such as string) data. These will not, because they are not designed to. I doubt that geomean is, either.
If you are getting the ‘Undefined function’ error, that means that ‘gm’ was not in your code before you called it. It is an anonymous function, so it must be close to the beginning of your code in order to use it later. It is not like a function file.
.
Walter Roberson
on 5 Mar 2023
The anonymous function gm is not designed to be a complete drop-in replacement for all functionality of geomean. Star Strider and I made no attempt to handle omitnan or the dimension arguments, and did not pay attention to calculations on matrices.
We also were not telling you that gm or gmL were already available in your version of MATLAB: we were providing a quick fix.
If you need more functionality then you should define your own gm.m using the mathematical logic we show, but perhaps looping over each column removing nan values before doing the calculation.
Walter Roberson
on 4 Mar 2023
0 votes
it needs the Statistics and Machine Learning toolbox
2 Comments
Adi Purwandana
on 5 Mar 2023
Walter Roberson
on 5 Mar 2023
Edited: Walter Roberson
on 5 Mar 2023
The software needs to be licensed and installed on whatever MATLAB you are executing the code with.
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!