changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/center.m

changeset 10289: 4b124317dc38
parent:f464119ec165
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (61 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006,
2## 2007, 2009 Kurt Hornik
3## Copyright (C) 2009 VZLU Prague
4##
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
9## the Free Software Foundation; either version 3 of the License, or (at
10## your option) any later version.
11##
12## Octave is distributed in the hope that it will be useful, but
13## WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15## General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with Octave; see the file COPYING. If not, see
19## <http://www.gnu.org/licenses/>.
20
21## -*- texinfo -*-
22## @deftypefn {Function File} {} center (@var{x})
23## @deftypefnx {Function File} {} center (@var{x}, @var{dim})
24## If @var{x} is a vector, subtract its mean.
25## If @var{x} is a matrix, do the above for each column.
26## If the optional argument @var{dim} is given, perform the above
27## operation along this dimension
28## @end deftypefn
29
30## Author: KH <Kurt.Hornik@wu-wien.ac.at>
31## Description: Center by subtracting means
32
33function retval = center (x, dim)
34
35 if (nargin != 1 && nargin != 2)
36 print_usage ();
37 endif
38
39 if (nargin < 2)
40 dim = [find(size (x) != 1, 1), 1](1); # First non-singleton dim.
41 endif
42 n = size (x, dim);
43
44 if (n == 0)
45 retval = x;
46 else
47 retval = bsxfun (@minus, x, sum (x, dim) / n);
48 endif
49endfunction