Mercurial > octave
annotate scripts/statistics/base/mean.m @ 10687:a8ce6bdecce5
Improve documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 20:22:38 -0700 |
parents | f0c3d3fc4903 |
children | 693e22af08ae |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, |
8920 | 2 ## 2006, 2007, 2008, 2009 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 -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
21 ## @deftypefn {Function File} {} mean (@var{x}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
22 ## @deftypefnx {Function File} {} mean (@var{x}, @var{dim}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
23 ## @deftypefnx {Function File} {} mean (@var{x}, @var{opt}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
24 ## @deftypefnx {Function File} {} mean (@var{x}, @var{dim}, @var{opt}) |
3367 | 25 ## If @var{x} is a vector, compute the mean of the elements of @var{x} |
26 ## @tex | |
27 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$ | |
28 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
29 ## @ifnottex |
3426 | 30 ## |
3367 | 31 ## @example |
32 ## mean (x) = SUM_i x(i) / N | |
33 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @end ifnottex |
3367 | 35 ## If @var{x} is a matrix, compute the mean for each column and return them |
36 ## in a row vector. | |
3200 | 37 ## |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
38 ## The optional argument @var{opt} selects the type of mean to compute. |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
39 ## The following options are recognized: |
3367 | 40 ## |
41 ## @table @code | |
42 ## @item "a" | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
43 ## Compute the (ordinary) arithmetic mean. [default] |
3367 | 44 ## |
45 ## @item "g" | |
7001 | 46 ## Compute the geometric mean. |
3367 | 47 ## |
48 ## @item "h" | |
49 ## Compute the harmonic mean. | |
50 ## @end table | |
4022 | 51 ## |
52 ## If the optional argument @var{dim} is supplied, work along dimension | |
53 ## @var{dim}. | |
54 ## | |
55 ## Both @var{dim} and @var{opt} are optional. If both are supplied, | |
56 ## either may appear first. | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
57 ## @seealso{median,mode} |
3367 | 58 ## @end deftypefn |
3426 | 59 |
5428 | 60 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 61 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200 | 62 |
4022 | 63 function y = mean (x, opt1, opt2) |
64 | |
65 need_dim = 0; | |
3200 | 66 |
4022 | 67 if (nargin == 1) |
68 opt = "a"; | |
69 need_dim = 1; | |
70 elseif (nargin == 2) | |
5443 | 71 if (ischar (opt1)) |
4022 | 72 opt = opt1; |
73 need_dim = 1; | |
74 else | |
4525 | 75 dim = opt1; |
4022 | 76 opt = "a"; |
77 endif | |
78 elseif (nargin == 3) | |
5443 | 79 if (ischar (opt1)) |
4022 | 80 opt = opt1; |
81 dim = opt2; | |
5443 | 82 elseif (ischar (opt2)) |
4022 | 83 opt = opt2; |
84 dim = opt1; | |
85 else | |
6046 | 86 error ("mean: expecting opt to be a string"); |
4022 | 87 endif |
88 else | |
6046 | 89 print_usage (); |
3200 | 90 endif |
3426 | 91 |
4022 | 92 if (need_dim) |
93 t = find (size (x) != 1); | |
94 if (isempty (t)) | |
95 dim = 1; | |
96 else | |
97 dim = t(1); | |
98 endif | |
3200 | 99 endif |
100 | |
5243 | 101 if (dim > ndims (x)) |
102 n = 1; | |
103 else | |
104 sz = size (x); | |
105 n = sz (dim); | |
106 endif | |
3426 | 107 |
3200 | 108 if (strcmp (opt, "a")) |
4022 | 109 y = sum (x, dim) / n; |
3200 | 110 elseif (strcmp (opt, "g")) |
8976
22a7e4690742
adjust some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
111 y = prod (x, dim) .^ (1/n); |
3200 | 112 elseif (strcmp (opt, "h")) |
4022 | 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 |