Mercurial > octave-nkf
annotate scripts/statistics/base/center.m @ 9744:fb3543975ed9
optimize center using bsxfun
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 20 Oct 2009 12:35:42 +0200 |
parents | f464119ec165 |
children | e0767a0965f1 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
8920 | 2 ## 2007, 2009 Kurt Hornik |
9744
fb3543975ed9
optimize center using bsxfun
Jaroslav Hajek <highegg@gmail.com>
parents:
8977
diff
changeset
|
3 ## Copyright (C) 2009 VZLU Prague |
3426 | 4 ## |
3922 | 5 ## This file is part of Octave. |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
3426 | 11 ## |
3922 | 12 ## Octave is distributed in the hope that it will be useful, but |
3200 | 13 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 15 ## General Public License for more details. |
16 ## | |
3200 | 17 ## You should have received a copy of the GNU General Public License |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
3200 | 20 |
3453 | 21 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {} center (@var{x}) | |
4844 | 23 ## @deftypefnx {Function File} {} center (@var{x}, @var{dim}) |
3453 | 24 ## If @var{x} is a vector, subtract its mean. |
25 ## If @var{x} is a matrix, do the above for each column. | |
4844 | 26 ## If the optional argument @var{dim} is given, perform the above |
27 ## operation along this dimension | |
3453 | 28 ## @end deftypefn |
3200 | 29 |
5428 | 30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 31 ## Description: Center by subtracting means |
3426 | 32 |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
33 function retval = center (x, dim) |
3426 | 34 |
4844 | 35 if (nargin != 1 && nargin != 2) |
6046 | 36 print_usage (); |
3200 | 37 endif |
38 | |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
39 if (nargin < 2) |
9744
fb3543975ed9
optimize center using bsxfun
Jaroslav Hajek <highegg@gmail.com>
parents:
8977
diff
changeset
|
40 dim = [find(size (x) != 1, 1), 1](1); # First non-singleton dim. |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
41 endif |
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
42 n = size (x, dim); |
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
43 |
9744
fb3543975ed9
optimize center using bsxfun
Jaroslav Hajek <highegg@gmail.com>
parents:
8977
diff
changeset
|
44 if (n == 0) |
fb3543975ed9
optimize center using bsxfun
Jaroslav Hajek <highegg@gmail.com>
parents:
8977
diff
changeset
|
45 retval = x; |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
46 else |
9744
fb3543975ed9
optimize center using bsxfun
Jaroslav Hajek <highegg@gmail.com>
parents:
8977
diff
changeset
|
47 retval = bsxfun (@minus, x, sum (x, dim) / n); |
3200 | 48 endif |
4844 | 49 endfunction |