This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

geomean

Geometric mean

Syntax

m = geomean(X)
m = geomean(X,'all')
m = geomean(X,dim)
m = geomean(X,vecdim)
m = geomean(___,nanflag)

Description

example

m = geomean(X) returns the geometric mean of X.

  • If X is a vector, then geomean(X) is the geometric mean of the elements in X.

  • If X is a matrix, then geomean(X) is a row vector containing the geometric mean of each column of X.

  • If X is a multidimensional array, then geomean operates along the first nonsingleton dimension of X.

example

m = geomean(X,'all') returns the geometric mean of all the elements in X.

example

m = geomean(X,dim) returns the geometric mean along the operating dimension dim of X.

example

m = geomean(X,vecdim) returns the geometric mean over the dimensions specified in the vector vecdim. For example, if X is a 2-by-3-by-4 array, then geomean(X,[1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the geometric mean of the elements on the corresponding page of X.

example

m = geomean(___,nanflag) specifies whether to exclude NaN values from the calculation, using any of the input argument combinations in previous syntaxes. By default, geomean includes NaN values in the calculation (nanflag has the value 'includenan'). To exclude NaN values, set the value of nanflag to 'omitnan'.

Examples

collapse all

Set the random seed for reproducibility of the results.

rng('default')

Create a matrix of exponential random numbers with 5 rows and 4 columns.

X = exprnd(1,5,4)
X = 5×4

    0.2049    2.3275    1.8476    1.9527
    0.0989    1.2783    0.0298    0.8633
    2.0637    0.6035    0.0438    0.0880
    0.0906    0.0434    0.7228    0.2329
    0.4583    0.0357    0.2228    0.0414

Compute the geometric and arithmetic means of the columns of X.

geometric = geomean(X)
geometric = 1×4

    0.2805    0.3083    0.2079    0.2698

arithmetic = mean(X)
arithmetic = 1×4

    0.5833    0.8577    0.5734    0.6357

The arithmetic mean is greater than the geometric mean for all the columns of X.

Find the geometric mean over multiple dimensions by using the 'all' input argument.

Create a 2-by-5-by-4 array X.

X = reshape(1:40,[2 5 4])
X = 
X(:,:,1) =

     1     3     5     7     9
     2     4     6     8    10


X(:,:,2) =

    11    13    15    17    19
    12    14    16    18    20


X(:,:,3) =

    21    23    25    27    29
    22    24    26    28    30


X(:,:,4) =

    31    33    35    37    39
    32    34    36    38    40

Find the geometric mean of all the elements of X.

m = geomean(X,'all')
m = 15.7685

m is the geometric mean of the entire array X.

Find the geometric mean along different operating dimensions and vectors of dimensions for a multidimensional array.

Create a 3-by-5-by-2 array X.

X = reshape(1:30,[3 5 2])
X = 
X(:,:,1) =

     1     4     7    10    13
     2     5     8    11    14
     3     6     9    12    15


X(:,:,2) =

    16    19    22    25    28
    17    20    23    26    29
    18    21    24    27    30

Find the geometric mean of X along the default dimension.

gmean1 = geomean(X)
gmean1 = 
gmean1(:,:,1) =

    1.8171    4.9324    7.9581   10.9696   13.9761


gmean1(:,:,2) =

   16.9804   19.9833   22.9855   25.9872   28.9885

By default, geomean operates along the first dimension of X whose size does not equal 1. In this case, this dimension is the first dimension of X. Therefore, gmean1 is a 1-by-5-by-2 array.

Find the geometric mean of X along the second dimension.

gmean2 = geomean(X,2)
gmean2 = 
gmean2(:,:,1) =

    5.1549
    6.5784
    7.8155


gmean2(:,:,2) =

   21.5814
   22.6004
   23.6177

gmean2 is a 3-by-1-by-2 array.

Find the geometric mean of X along the third dimension.

gmean3 = geomean(X,3)
gmean3 = 3×5

    4.0000    8.7178   12.4097   15.8114   19.0788
    5.8310   10.0000   13.5647   16.9115   20.1494
    7.3485   11.2250   14.6969   18.0000   21.2132

gmean3 is a 3-by-5 array.

Find the geometric mean of each page of X by specifying the first and second dimensions using the vecdim input argument.

mpage = geomean(X,[1 2])
mpage = 
mpage(:,:,1) =

    6.4234


mpage(:,:,2) =

   22.5845

For example, mpage(1,1,2) is the geometric mean of the elements in X(:,:,2).

Find the geometric mean of the elements in each X(i,:,:) slice by specifying the second and third dimensions.

mrow = geomean(X,[2 3])
mrow = 3×1

   10.5475
   12.1932
   13.5862

For example, mrow(3) is the geometric mean of the elements in X(3,:,:), and is equivalent to specifying geomean(X(3,:,:),'all').

Create a vector and compute its geomean, excluding NaN values.

x = 1:10;
x(3) = nan; % Replace the third element of x with a NaN value
n = geomean(x,'omitnan')
n = 4.7408

If you do not specify 'omitnan', then geomean(x) returns NaN.

Input Arguments

collapse all

Input data that represents a sample from a population, specified as a nonnegative vector, matrix, or multidimensional array.

  • If X is a vector, then geomean(X) is the geometric mean of the elements in X.

  • If X is a matrix, then geomean(X) is a row vector containing the geometric mean of each column of X.

  • If X is a multidimensional array, then geomean operates along the first nonsingleton dimension of X.

To specify the operating dimension when X is a matrix or an array, use the dim input argument.

Data Types: single | double

Dimension along which to operate, specified as a positive integer scalar. If you do not specify a value, then the default value is the first array dimension of X whose size does not equal 1.

Consider a two-dimensional array X:

  • If dim is equal to 1, then geomean(X,1) returns a row vector containing the geometric mean for each column in X.

  • If dim is equal to 2, then geomean(X,2) returns a column vector containing the geometric mean for each row in X.

If dim is greater than ndims(X) or if size(X,dim) is 1, then geomean returns X.

Data Types: single | double

Vector of dimensions, specified as a positive integer vector. Each element of vecdim represents a dimension of the input array X. The output m has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and m.

For example, if X is a 2-by-3-by-3 array, then geomean(X,[1 2]) returns a 1-by-1-by-3 array. Each element of the output is the geometric mean of the elements on the corresponding page of X.

Data Types: single | double

NaN condition, specified as one of these values:

  • 'includenan' — Include NaN values when computing the geomean. This returns NaN.

  • 'omitnan' — Ignore NaN values in the input.

Data Types: char | string

Output Arguments

collapse all

Geometric mean, returned as a scalar, vector, matrix, or multidimensional array.

More About

collapse all

Geometric Mean

The geometric mean of a sample X is

m=[i=1nxi]1n

where n is the number of values in X.

Extended Capabilities

Introduced before R2006a