1 function [y]=mean(x,DIM,opt,W)
2 % MEAN calculates the mean of data elements.
4 % y = mean(x [,DIM] [,opt] [, W])
9 % N MEAN of N-th dimension
10 % default or []: first DIMENSION, with more than 1 element
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)
28 % mean(x,DIM,opt,W); '
31 % - can deal with NaN's (missing values)
33 % - dimension argument also in Octave
34 % - compatible to Matlab and Octave
36 % see also: SUMSKIPNAN, MEAN, GEOMEAN, HARMMEAN
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/
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.
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.
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/>.
64 %if ~isnumeric(DIM), %>=65;%abs('A'),
65 if (DIM>64) %abs('A'),
66 %------ case: mean(x,opt)
70 %------ case: mean(x,DIM)
74 if isnumeric(DIM) && isnumeric(opt)
75 %------ case: mean(x,DIM,W)
78 elseif (DIM>64) %abs('A'),
79 %------ case: mean(x,opt,DIM)
80 %if ~isnumeric(DIM), %>=65;%abs('A'),
86 %------ case: mean(x,DIM,opt)
90 %------ case: mean(x,DIM,opt,W)
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); '
99 elseif any(opt=='aAgGhH')
100 opt = upper(opt); % eliminate old version
102 error('Error MEAN: invalid opt argument');
106 [y, n] = sumskipnan(x,DIM,W);
109 [y, n] = sumskipnan(log(x),DIM,W);
112 [y, n] = sumskipnan(1./x,DIM,W);
115 fprintf (2,'mean: option `%s` not recognized', opt);
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)