Mercurial > octave
annotate scripts/statistics/base/mean.m @ 8517:81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
author | sh@sh-laptop |
---|---|
date | Wed, 14 Jan 2009 20:44:25 -0500 |
parents | 83a8781b529d |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, |
2 ## 2006, 2007 Kurt Hornik | |
3426 | 3 ## |
3922 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3200 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3200 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3367 | 20 ## -*- texinfo -*- |
4022 | 21 ## @deftypefn {Function File} {} mean (@var{x}, @var{dim}, @var{opt}) |
3367 | 22 ## If @var{x} is a vector, compute the mean of the elements of @var{x} |
23 ## @iftex | |
24 ## @tex | |
25 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$ | |
26 ## @end tex | |
27 ## @end iftex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
28 ## @ifnottex |
3426 | 29 ## |
3367 | 30 ## @example |
31 ## mean (x) = SUM_i x(i) / N | |
32 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
33 ## @end ifnottex |
3367 | 34 ## If @var{x} is a matrix, compute the mean for each column and return them |
35 ## in a row vector. | |
3200 | 36 ## |
3367 | 37 ## With the optional argument @var{opt}, the kind of mean computed can be |
38 ## selected. The following options are recognized: | |
39 ## | |
40 ## @table @code | |
41 ## @item "a" | |
42 ## Compute the (ordinary) arithmetic mean. This is the default. | |
43 ## | |
44 ## @item "g" | |
7001 | 45 ## Compute the geometric mean. |
3367 | 46 ## |
47 ## @item "h" | |
48 ## Compute the harmonic mean. | |
49 ## @end table | |
4022 | 50 ## |
51 ## If the optional argument @var{dim} is supplied, work along dimension | |
52 ## @var{dim}. | |
53 ## | |
54 ## Both @var{dim} and @var{opt} are optional. If both are supplied, | |
55 ## either may appear first. | |
3367 | 56 ## @end deftypefn |
3426 | 57 |
5428 | 58 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 59 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200 | 60 |
4022 | 61 function y = mean (x, opt1, opt2) |
62 | |
63 need_dim = 0; | |
3200 | 64 |
4022 | 65 if (nargin == 1) |
66 opt = "a"; | |
67 need_dim = 1; | |
68 elseif (nargin == 2) | |
5443 | 69 if (ischar (opt1)) |
4022 | 70 opt = opt1; |
71 need_dim = 1; | |
72 else | |
4525 | 73 dim = opt1; |
4022 | 74 opt = "a"; |
75 endif | |
76 elseif (nargin == 3) | |
5443 | 77 if (ischar (opt1)) |
4022 | 78 opt = opt1; |
79 dim = opt2; | |
5443 | 80 elseif (ischar (opt2)) |
4022 | 81 opt = opt2; |
82 dim = opt1; | |
83 else | |
6046 | 84 error ("mean: expecting opt to be a string"); |
4022 | 85 endif |
86 else | |
6046 | 87 print_usage (); |
3200 | 88 endif |
3426 | 89 |
4022 | 90 if (need_dim) |
91 t = find (size (x) != 1); | |
92 if (isempty (t)) | |
93 dim = 1; | |
94 else | |
95 dim = t(1); | |
96 endif | |
3200 | 97 endif |
98 | |
5243 | 99 if (dim > ndims (x)) |
100 n = 1; | |
101 else | |
102 sz = size (x); | |
103 n = sz (dim); | |
104 endif | |
3426 | 105 |
3200 | 106 if (strcmp (opt, "a")) |
4022 | 107 y = sum (x, dim) / n; |
3200 | 108 elseif (strcmp (opt, "g")) |
4022 | 109 x(x <= 0) = NaN; |
110 y = exp (sum (log (x), dim) / n); | |
3200 | 111 elseif (strcmp (opt, "h")) |
4022 | 112 x(x == 0) = NaN; |
113 y = n ./ sum (1 ./ x, dim); | |
3200 | 114 else |
3456 | 115 error ("mean: option `%s' not recognized", opt); |
3200 | 116 endif |
3426 | 117 |
3200 | 118 endfunction |
7411 | 119 |
120 %!test | |
121 %! x = -10:10; | |
122 %! y = x'; | |
123 %! z = [y, y+10]; | |
124 %! assert(mean (x) == 0 && mean (y) == 0 && mean (z) == [0, 10]); | |
125 | |
126 %!error mean (); | |
127 | |
128 %!error mean (1, 2, 3); | |
129 |