1 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} mean (@var{x})
21 ## @deftypefnx {Function File} {} mean (@var{x}, @var{dim})
22 ## @deftypefnx {Function File} {} mean (@var{x}, @var{opt})
23 ## @deftypefnx {Function File} {} mean (@var{x}, @var{dim}, @var{opt})
24 ## Compute the mean of the elements of the vector @var{x}.
26 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$
31 ## mean (x) = SUM_i x(i) / N
35 ## If @var{x} is a matrix, compute the mean for each column and return them
38 ## The optional argument @var{opt} selects the type of mean to compute.
39 ## The following options are recognized:
43 ## Compute the (ordinary) arithmetic mean. [default]
46 ## Compute the geometric mean.
49 ## Compute the harmonic mean.
52 ## If the optional argument @var{dim} is given, operate along this dimension.
54 ## Both @var{dim} and @var{opt} are optional. If both are supplied,
55 ## either may appear first.
56 ## @seealso{median, mode}
59 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
60 ## Description: Compute arithmetic, geometric, and harmonic mean
62 function y = mean (x, opt1, opt2)
64 if (nargin < 1 || nargin > 3)
68 if (! (isnumeric (x) || islogical (x)))
69 error ("mean: X must be a numeric vector or matrix");
89 elseif (ischar (opt2))
93 error ("mean: OPT must be a string");
102 ## Find the first non-singleton dimension.
103 (dim = find (sz > 1, 1)) || (dim = 1);
105 if (!(isscalar (dim) && dim == fix (dim))
106 || !(1 <= dim && dim <= nd))
107 error ("mean: DIM must be an integer and a valid dimension");
113 if (strcmp (opt, "a"))
114 y = sum (x, dim) / n;
115 elseif (strcmp (opt, "g"))
116 y = prod (x, dim) .^ (1/n);
117 elseif (strcmp (opt, "h"))
118 y = n ./ sum (1 ./ x, dim);
120 error ("mean: option `%s' not recognized", opt);
130 %! assert(mean (x) == 0);
131 %! assert(mean (y) == 0);
132 %! assert(mean (z) == [0, 10]);
134 %!assert(mean (magic(3), 1), [5, 5, 5]);
135 %!assert(mean (magic(3), 2), [5; 5; 5]);
136 %!assert(mean ([2 8], 'g'), 4);
137 %!assert(mean ([4 4 2], 'h'), 3);
138 %!assert(mean (logical ([1 0 1 1])), 0.75);
139 %!assert(mean (single ([1 0 1 1])), single (0.75));
141 %% Test input validation
143 %!error mean (1, 2, 3, 4);
144 %!error mean ({1:5});
145 %!error mean (1, 2, 3);
146 %!error mean (1, ones(2,2));
147 %!error mean (1, 1.5);
150 %!error mean (1, 'b');