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').