]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/mean.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / mean.m
1 function [y]=mean(x,DIM,opt,W)
2 % MEAN calculates the mean of data elements. 
3
4 %  y = mean(x [,DIM] [,opt] [, W])
5 %
6 % DIM   dimension
7 %       1 MEAN of columns
8 %       2 MEAN of rows
9 %       N MEAN of  N-th dimension 
10 %       default or []: first DIMENSION, with more than 1 element
11 %
12 % opt   options 
13 %       'A' arithmetic mean
14 %       'G' geometric mean
15 %       'H' harmonic mean
16 %
17 % W     weights to compute weighted mean (default: [])
18 %       if W=[], all weights are 1. 
19 %       number of elements in W must match size(x,DIM) 
20 %
21 % usage: 
22 %       mean(x)
23 %       mean(x,DIM)
24 %       mean(x,opt)
25 %       mean(x,opt,DIM)
26 %       mean(x,DIM,opt)
27 %       mean(x,DIM,W)
28 %       mean(x,DIM,opt,W); '
29 %
30 % features:
31 % - can deal with NaN's (missing values)
32 % - weighting of data 
33 % - dimension argument also in Octave
34 % - compatible to Matlab and Octave
35 %
36 % see also: SUMSKIPNAN, MEAN, GEOMEAN, HARMMEAN
37 %
38
39 %       $Id: mean.m 9033 2011-11-08 20:58:07Z schloegl $
40 %       Copyright (C) 2000-2004,2008,2009,2011 by Alois Schloegl <alois.schloegl@gmail.com>     
41 %       This is part of the NaN-toolbox. For more details see
42 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
43 %
44 %    This program is free software; you can redistribute it and/or modify
45 %    it under the terms of the GNU General Public License as published by
46 %    the Free Software Foundation; either version 3 of the License, or
47 %    (at your option) any later version.
48 %
49 %    This program is distributed in the hope that it will be useful,
50 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
51 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52 %    GNU General Public License for more details.
53 %
54 %    You should have received a copy of the GNU General Public License
55 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
56
57 if nargin==1,
58         %------ case:  mean(x)
59         W = []; 
60         DIM=[]; 
61         opt='a';
62 elseif (nargin==2)
63         W = []; 
64         %if ~isnumeric(DIM), %>=65;%abs('A'), 
65         if (DIM>64) %abs('A'), 
66                 %------ case:  mean(x,opt)
67                 opt=DIM;
68                 DIM=[]; 
69         else
70                 %------ case:  mean(x,DIM)
71                 opt='a';
72         end;    
73 elseif (nargin == 3), 
74         if isnumeric(DIM) && isnumeric(opt)
75                 %------ case:  mean(x,DIM,W)
76                 W = opt; 
77                 opt='a';
78         elseif (DIM>64) %abs('A'), 
79                 %------ case:  mean(x,opt,DIM)
80                 %if ~isnumeric(DIM), %>=65;%abs('A'), 
81                 tmp=opt;
82                 opt=DIM;
83                 DIM=tmp;
84                 W = []; 
85         else 
86                 %------ case:  mean(x,DIM,opt)
87                 W = [];
88         end;
89 elseif nargin==4,
90                 %------ case: mean(x,DIM,opt,W)
91         ; 
92 else
93         help mean 
94 %       fprintf(1,'usage: mean(x) or mean(x,DIM) or mean(x,opt,DIM) or mean(x,DIM,opt) or mean(x,DIM,W) or mean(x,DIM,opt,W); '
95 end;
96
97 if isempty(opt)
98         opt = 'A';
99 elseif any(opt=='aAgGhH')
100         opt = upper(opt); % eliminate old version 
101 else 
102         error('Error MEAN: invalid opt argument');
103 end; 
104
105 if  (opt == 'A')
106         [y, n] = sumskipnan(x,DIM,W);
107         y = y./n;
108 elseif (opt == 'G')
109         [y, n] = sumskipnan(log(x),DIM,W);
110         y = exp (y./n);
111 elseif (opt == 'H')
112         [y, n] = sumskipnan(1./x,DIM,W);
113         y = n./y;
114 else
115         fprintf (2,'mean:  option `%s` not recognized', opt);
116 end 
117
118 %!assert(mean([1,NaN],1),[1,NaN])
119 %!assert(mean([1,NaN],2),1)
120 %!assert(mean([+inf,-inf]),NaN)
121 %!assert(mean([+0,-0],'h'),NaN)
122 %!assert(mean([1,4,NaN],'g'),2)
123       
124       
125