3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
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 |
|
28 ## @ifinfo |
3426
|
29 ## |
3367
|
30 ## @example |
|
31 ## mean (x) = SUM_i x(i) / N |
|
32 ## @end example |
|
33 ## @end ifinfo |
|
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" |
|
45 ## Computer the geometric mean. |
|
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 |
3456
|
58 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
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) |
|
69 if (isstr (opt1)) |
|
70 opt = opt1; |
|
71 need_dim = 1; |
|
72 else |
4525
|
73 dim = opt1; |
4022
|
74 opt = "a"; |
|
75 endif |
|
76 elseif (nargin == 3) |
|
77 if (isstr (opt1)) |
|
78 opt = opt1; |
|
79 dim = opt2; |
|
80 elseif (isstr (opt2)) |
|
81 opt = opt2; |
|
82 dim = opt1; |
|
83 else |
|
84 usage ("mean: expecting opt to be a string"); |
|
85 endif |
|
86 else |
|
87 usage ("mean (x, dim, opt) or mean (x, dim, opt)"); |
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 |