Undefined function 'geomean' for input arguments of type 'double' in Matlab 2020

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

The geomean function is part of Statistics and Machine Learning Toolbox. Do you have this toolbox installed and licensed? You can check this by running the ver command and looking for a line starting with "Statistics and Machine Learning Toolbox" in the displayed output.
If that line does not exist you will need to install Statistics and Machine Learning Toolbox to use this function.

More Answers (2)

An alternative is:
gm = @(x) prod(x)^(1/numel(x));
x = rand(1,100);
y1 = geomean(x)
y1 = 0.3765
y2 = gm(x)
y2 = 0.3765
.

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)
y1 =
3477.14584289168
y2 = gm(x)
y2 =
Inf
y3 = gmL(x)
y3 =
3477.14584289168
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.
I believe that is part of its general definition, considering its geometric origins: Geometric mean (Wikipedia). Triangles cannot have negative sides.
I deal with very small number (order of 1e-10 to 1e-1) and gm function didn't work too....
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)]
ans = 1×2
8.39162752299003e-11 0.0515974360571979
y1 = geomean(x)
y1 =
0.000144784482174426
y2 = gm(x)
y2 =
0.000144784482174426
y3 = gmL(x)
y3 =
0.000144784482174426
I combine the line with "omitnan" when I executed it in Matlab 2022 (with licensed and installed stat mach.learning there). It results:
Undefined function 'gm' for input arguments of type 'string'
Then, I tried to just write gm(X), it results:
Undefined function 'gm' for input arguments of type 'double'
Anyway, it's fine when I just use geomean(X,'omitnan") on this Matlab 22. I just curious then, why it failed to use gm on both my 2021 and 2022 Matlab.
For additional info, all the cases above were tried on my app designer. When I try in command window, all geomean function (geomean, gm, gmL) work normal... Sorry for not mentioning this previously.
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.
.
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.

Sign in to comment.

it needs the Statistics and Machine Learning toolbox

2 Comments

I checked my my licenses, the toolbox has been installed on my 2022 academic license but not in my personal license (2021).
The software needs to be licensed and installed on whatever MATLAB you are executing the code with.

Sign in to comment.

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!