Mercurial > octave-nkf
diff scripts/statistics/base/center.m @ 4844:9f7ef92b50b0
[project @ 2004-04-02 17:26:53 by jwe]
author | jwe |
---|---|
date | Fri, 02 Apr 2004 17:26:54 +0000 |
parents | 22bd65326ec1 |
children | 4c8a2e4e0717 |
line wrap: on
line diff
--- a/scripts/statistics/base/center.m Fri Apr 02 14:54:20 2004 +0000 +++ b/scripts/statistics/base/center.m Fri Apr 02 17:26:54 2004 +0000 @@ -19,27 +19,40 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} center (@var{x}) +## @deftypefnx {Function File} {} center (@var{x}, @var{dim}) ## If @var{x} is a vector, subtract its mean. ## If @var{x} is a matrix, do the above for each column. +## If the optional argument @var{dim} is given, perform the above +## operation along this dimension ## @end deftypefn ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> ## Description: Center by subtracting means -function retval = center (x) +function retval = center (x, varargin) - if (nargin != 1) + if (nargin != 1 && nargin != 2) usage ("center (x)"); endif if (isvector (x)) - retval = x - mean (x); + retval = x - mean (x, varargin{:}); elseif (ismatrix (x)) - retval = x - ones (rows (x), 1) * mean (x); + if nargin < 2 + dim = min (find (size (x) > 1)); + if isempty (dim), + dim=1; + endif; + else + dim = varargin {1}; + endif + sz = ones (1, ndims (x)); + sz (dim) = size (x, dim); + retval = x - repmat (mean (x, dim), sz); elseif (isempty (x)) retval = x; else error ("center: x must be a vector or a matrix"); endif -endfunction \ No newline at end of file +endfunction