Mercurial > hg > octave-nkf
comparison scripts/statistics/base/center.m @ 8977:f464119ec165
further simplify some stats funcs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 13 Mar 2009 21:26:13 +0100 |
parents | eb63fbe60fab |
children | fb3543975ed9 |
comparison
equal
deleted
inserted
replaced
8976:22a7e4690742 | 8977:f464119ec165 |
---|---|
27 ## @end deftypefn | 27 ## @end deftypefn |
28 | 28 |
29 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> | 29 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
30 ## Description: Center by subtracting means | 30 ## Description: Center by subtracting means |
31 | 31 |
32 function retval = center (x, varargin) | 32 function retval = center (x, dim) |
33 | 33 |
34 if (nargin != 1 && nargin != 2) | 34 if (nargin != 1 && nargin != 2) |
35 print_usage (); | 35 print_usage (); |
36 endif | 36 endif |
37 | 37 |
38 if (isvector (x)) | 38 if (nargin < 2) |
39 retval = x - mean (x, varargin{:}); | 39 t = find (size (x) != 1); |
40 elseif (ismatrix (x)) | 40 if (isempty (t)) |
41 if nargin < 2 | 41 dim = 1; |
42 dim = find (size (x) > 1, 1); | |
43 if (isempty (dim)) | |
44 dim = 1; | |
45 endif; | |
46 else | 42 else |
47 dim = varargin{1}; | 43 dim = t(1); |
48 endif | 44 endif |
49 sz = ones (1, ndims (x)); | 45 endif |
50 sz (dim) = size (x, dim); | 46 n = size (x, dim); |
51 retval = x - repmat (mean (x, dim), sz); | 47 |
52 elseif (isempty (x)) | 48 if (n == 1) |
49 retval = zeros (size (x)); | |
50 elseif (n > 0) | |
51 if (isvector (x)) | |
52 retval = x - sum (x) / n; | |
53 else | |
54 mx = sum (x, dim) / n; | |
55 idx(1:ndims (x)) = {':'}; | |
56 idx{dim} = ones (1, n); | |
57 retval = x - mx(idx{:}); | |
58 endif | |
59 else | |
53 retval = x; | 60 retval = x; |
54 else | |
55 error ("center: x must be a vector or a matrix"); | |
56 endif | 61 endif |
57 | |
58 endfunction | 62 endfunction |