X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fnan-2.5.5%2Fmean.m;fp=octave_packages%2Fnan-2.5.5%2Fmean.m;h=139cb136591bd91bb70f96847cf2dd74f8b365f7;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/nan-2.5.5/mean.m b/octave_packages/nan-2.5.5/mean.m new file mode 100644 index 0000000..139cb13 --- /dev/null +++ b/octave_packages/nan-2.5.5/mean.m @@ -0,0 +1,125 @@ +function [y]=mean(x,DIM,opt,W) +% MEAN calculates the mean of data elements. +% +% y = mean(x [,DIM] [,opt] [, W]) +% +% DIM dimension +% 1 MEAN of columns +% 2 MEAN of rows +% N MEAN of N-th dimension +% default or []: first DIMENSION, with more than 1 element +% +% opt options +% 'A' arithmetic mean +% 'G' geometric mean +% 'H' harmonic mean +% +% W weights to compute weighted mean (default: []) +% if W=[], all weights are 1. +% number of elements in W must match size(x,DIM) +% +% usage: +% mean(x) +% mean(x,DIM) +% mean(x,opt) +% mean(x,opt,DIM) +% mean(x,DIM,opt) +% mean(x,DIM,W) +% mean(x,DIM,opt,W); ' +% +% features: +% - can deal with NaN's (missing values) +% - weighting of data +% - dimension argument also in Octave +% - compatible to Matlab and Octave +% +% see also: SUMSKIPNAN, MEAN, GEOMEAN, HARMMEAN +% + +% $Id: mean.m 9033 2011-11-08 20:58:07Z schloegl $ +% Copyright (C) 2000-2004,2008,2009,2011 by Alois Schloegl +% This is part of the NaN-toolbox. For more details see +% http://pub.ist.ac.at/~schloegl/matlab/NaN/ +% +% This program is free software; you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation; either version 3 of the License, or +% (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program; If not, see . + +if nargin==1, + %------ case: mean(x) + W = []; + DIM=[]; + opt='a'; +elseif (nargin==2) + W = []; + %if ~isnumeric(DIM), %>=65;%abs('A'), + if (DIM>64) %abs('A'), + %------ case: mean(x,opt) + opt=DIM; + DIM=[]; + else + %------ case: mean(x,DIM) + opt='a'; + end; +elseif (nargin == 3), + if isnumeric(DIM) && isnumeric(opt) + %------ case: mean(x,DIM,W) + W = opt; + opt='a'; + elseif (DIM>64) %abs('A'), + %------ case: mean(x,opt,DIM) + %if ~isnumeric(DIM), %>=65;%abs('A'), + tmp=opt; + opt=DIM; + DIM=tmp; + W = []; + else + %------ case: mean(x,DIM,opt) + W = []; + end; +elseif nargin==4, + %------ case: mean(x,DIM,opt,W) + ; +else + help mean +% 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); ' +end; + +if isempty(opt) + opt = 'A'; +elseif any(opt=='aAgGhH') + opt = upper(opt); % eliminate old version +else + error('Error MEAN: invalid opt argument'); +end; + +if (opt == 'A') + [y, n] = sumskipnan(x,DIM,W); + y = y./n; +elseif (opt == 'G') + [y, n] = sumskipnan(log(x),DIM,W); + y = exp (y./n); +elseif (opt == 'H') + [y, n] = sumskipnan(1./x,DIM,W); + y = n./y; +else + fprintf (2,'mean: option `%s` not recognized', opt); +end + +%!assert(mean([1,NaN],1),[1,NaN]) +%!assert(mean([1,NaN],2),1) +%!assert(mean([+inf,-inf]),NaN) +%!assert(mean([+0,-0],'h'),NaN) +%!assert(mean([1,4,NaN],'g'),2) + + +