changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/mean.m

changeset 10289: 4b124317dc38
parent:22a7e4690742
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (70 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005,
2## 2006, 2007, 2008, 2009 Kurt Hornik
3##
4## This file is part of Octave.
5##
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.
10##
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.
15##
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/>.
19
20## -*- texinfo -*-
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}
23## @tex
24## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$
25## @end tex
26## @ifnottex
27##
28## @example
29## mean (x) = SUM_i x(i) / N
30## @end example
31## @end ifnottex
32## If @var{x} is a matrix, compute the mean for each column and return them
33## in a row vector.
34##
35## With the optional argument @var{opt}, the kind of mean computed can be
36## selected. The following options are recognized:
37##
38## @table @code
39## @item "a"
40## Compute the (ordinary) arithmetic mean. This is the default.
41##
42## @item "g"
43## Compute the geometric mean.
44##
45## @item "h"
46## Compute the harmonic mean.
47## @end table
48##
49## If the optional argument @var{dim} is supplied, work along dimension
50## @var{dim}.
51##
52## Both @var{dim} and @var{opt} are optional. If both are supplied,
53## either may appear first.
54## @end deftypefn
55
56## Author: KH <Kurt.Hornik@wu-wien.ac.at>
57## Description: Compute arithmetic, geometric, and harmonic mean
58
59function y = mean (x, opt1, opt2)
60
61 need_dim = 0;
62
63 if (nargin == 1)
64 opt = "a";
65 need_dim = 1;
66 elseif (nargin == 2)
67 if (ischar (opt1))
68 opt = opt1;
69 need_dim = 1;
70 else
71 dim = opt1;
72 opt = "a";
73 endif
74 elseif (nargin == 3)
75 if (ischar (opt1))
76 opt = opt1;
77 dim = opt2;
78 elseif (ischar (opt2))
79 opt = opt2;
80 dim = opt1;
81 else
82 error ("mean: expecting opt to be a string");
83 endif
84 else
85 print_usage ();
86 endif
87
88 if (need_dim)
89 t = find (size (x) != 1);
90 if (isempty (t))
91 dim = 1;
92 else
93 dim = t(1);
94 endif
95 endif
96
97 if (dim > ndims (x))
98 n = 1;
99 else
100 sz = size (x);
101 n = sz (dim);
102 endif
103
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);
110 else
111 error ("mean: option `%s' not recognized", opt);
112 endif
113
114endfunction
115
116%!test
117%! x = -10:10;
118%! y = x';
119%! z = [y, y+10];
120%! assert(mean (x) == 0 && mean (y) == 0 && mean (z) == [0, 10]);
121
122%!error mean ();
123
124%!error mean (1, 2, 3);
125