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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) 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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3367
|
19 ## -*- texinfo -*- |
4022
|
20 ## @deftypefn {Function File} {} mean (@var{x}, @var{dim}, @var{opt}) |
3367
|
21 ## If @var{x} is a vector, compute the mean of the elements of @var{x} |
|
22 ## @iftex |
|
23 ## @tex |
|
24 ## $$ {\rm mean}(x) = \bar{x} = {1\over N} \sum_{i=1}^N x_i $$ |
|
25 ## @end tex |
|
26 ## @end iftex |
|
27 ## @ifinfo |
3426
|
28 ## |
3367
|
29 ## @example |
|
30 ## mean (x) = SUM_i x(i) / N |
|
31 ## @end example |
|
32 ## @end ifinfo |
|
33 ## If @var{x} is a matrix, compute the mean for each column and return them |
|
34 ## in a row vector. |
3200
|
35 ## |
3367
|
36 ## With the optional argument @var{opt}, the kind of mean computed can be |
|
37 ## selected. The following options are recognized: |
|
38 ## |
|
39 ## @table @code |
|
40 ## @item "a" |
|
41 ## Compute the (ordinary) arithmetic mean. This is the default. |
|
42 ## |
|
43 ## @item "g" |
7001
|
44 ## Compute the geometric mean. |
3367
|
45 ## |
|
46 ## @item "h" |
|
47 ## Compute the harmonic mean. |
|
48 ## @end table |
4022
|
49 ## |
|
50 ## If the optional argument @var{dim} is supplied, work along dimension |
|
51 ## @var{dim}. |
|
52 ## |
|
53 ## Both @var{dim} and @var{opt} are optional. If both are supplied, |
|
54 ## either may appear first. |
3367
|
55 ## @end deftypefn |
3426
|
56 |
5428
|
57 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
58 ## Description: Compute arithmetic, geometric, and harmonic mean |
3200
|
59 |
4022
|
60 function y = mean (x, opt1, opt2) |
|
61 |
|
62 need_dim = 0; |
3200
|
63 |
4022
|
64 if (nargin == 1) |
|
65 opt = "a"; |
|
66 need_dim = 1; |
|
67 elseif (nargin == 2) |
5443
|
68 if (ischar (opt1)) |
4022
|
69 opt = opt1; |
|
70 need_dim = 1; |
|
71 else |
4525
|
72 dim = opt1; |
4022
|
73 opt = "a"; |
|
74 endif |
|
75 elseif (nargin == 3) |
5443
|
76 if (ischar (opt1)) |
4022
|
77 opt = opt1; |
|
78 dim = opt2; |
5443
|
79 elseif (ischar (opt2)) |
4022
|
80 opt = opt2; |
|
81 dim = opt1; |
|
82 else |
6046
|
83 error ("mean: expecting opt to be a string"); |
4022
|
84 endif |
|
85 else |
6046
|
86 print_usage (); |
3200
|
87 endif |
3426
|
88 |
4022
|
89 if (need_dim) |
|
90 t = find (size (x) != 1); |
|
91 if (isempty (t)) |
|
92 dim = 1; |
|
93 else |
|
94 dim = t(1); |
|
95 endif |
3200
|
96 endif |
|
97 |
5243
|
98 if (dim > ndims (x)) |
|
99 n = 1; |
|
100 else |
|
101 sz = size (x); |
|
102 n = sz (dim); |
|
103 endif |
3426
|
104 |
3200
|
105 if (strcmp (opt, "a")) |
4022
|
106 y = sum (x, dim) / n; |
3200
|
107 elseif (strcmp (opt, "g")) |
4022
|
108 x(x <= 0) = NaN; |
|
109 y = exp (sum (log (x), dim) / n); |
3200
|
110 elseif (strcmp (opt, "h")) |
4022
|
111 x(x == 0) = NaN; |
|
112 y = n ./ sum (1 ./ x, dim); |
3200
|
113 else |
3456
|
114 error ("mean: option `%s' not recognized", opt); |
3200
|
115 endif |
3426
|
116 |
3200
|
117 endfunction |