annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
2 ## 2007, 2009 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3876
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3876
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3876
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3876
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3876
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
15 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
20 ## -*- texinfo -*-
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
21 ## @deftypefn {Function File} {} center (@var{x})
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
22 ## @deftypefnx {Function File} {} center (@var{x}, @var{dim})
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
23 ## If @var{x} is a vector, subtract its mean.
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
24 ## If @var{x} is a matrix, do the above for each column.
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
25 ## If the optional argument @var{dim} is given, perform the above
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
26 ## operation along this dimension
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
27 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
28
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
29 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
30 ## Description: Center by subtracting means
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
31
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
32 function retval = center (x, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
33
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
34 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
35 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
36 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
37
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
38 if (nargin < 2)
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
39 t = find (size (x) != 1);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
40 if (isempty (t))
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
41 dim = 1;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
42 else
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
43 dim = t(1);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
44 endif
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
45 endif
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
46 n = size (x, dim);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
47
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
48 if (n == 1)
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
49 retval = zeros (size (x));
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
50 elseif (n > 0)
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
51 if (isvector (x))
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
52 retval = x - sum (x) / n;
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
53 else
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
54 mx = sum (x, dim) / n;
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
55 idx(1:ndims (x)) = {':'};
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
56 idx{dim} = ones (1, n);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
57 retval = x - mx(idx{:});
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
58 endif
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
59 else
3876
a134e5cfdc01 [project @ 2002-03-07 18:57:46 by jwe]
jwe
parents: 3458
diff changeset
60 retval = x;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61 endif
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4030
diff changeset
62 endfunction