1## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005,
2## 2006, 2007, 2008, 2009 Kurt Hornik
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {} mean (@var{x}, @var{dim}, @var{opt})
22## If @var{x} is a vector, compute the mean of the elements of @var{x}
24## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$
29## mean (x) = SUM_i x(i) / N
32## If @var{x} is a matrix, compute the mean for each column and return them
35## With the optional argument @var{opt}, the kind of mean computed can be
36## selected. The following options are recognized:
40## Compute the (ordinary) arithmetic mean. This is the default.
43## Compute the geometric mean.
46## Compute the harmonic mean.
49## If the optional argument @var{dim} is supplied, work along dimension
52## Both @var{dim} and @var{opt} are optional. If both are supplied,
53## either may appear first.
56## Author: KH <Kurt.Hornik@wu-wien.ac.at>
57## Description: Compute arithmetic, geometric, and harmonic mean
59function y = mean (x, opt1, opt2)
78 elseif (ischar (opt2))
82 error ("mean: expecting opt to be a string");
89 t = find (size (x) != 1);
104 if (strcmp (opt, "a"))
105 y = sum (x, dim) / n;
106 elseif (strcmp (opt, "g"))
107 y = prod (x, dim) .^ (1/n);
108 elseif (strcmp (opt, "h"))
109 y = n ./ sum (1 ./ x, dim);
111 error ("mean: option `%s' not recognized", opt);
120%! assert(mean (x) == 0 && mean (y) == 0 && mean (z) == [0, 10]);
124%!error mean (1, 2, 3);