Mercurial > hg > octave-lyh
annotate scripts/statistics/base/studentize.m @ 10669:cab3b148d4e4
Improve validation of input arguments for base statistics functions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Thu, 27 May 2010 20:12:51 -0700 |
parents | 95c3e38098bf |
children | fe3c3dfc07eb |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005, |
9245 | 2 ## 2006, 2007, 2009 Kurt Hornik |
3426 | 3 ## |
3922 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3426 | 10 ## |
3922 | 11 ## Octave is distributed in the hope that it will be useful, but |
3200 | 12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 14 ## General Public License for more details. |
15 ## | |
3200 | 16 ## You should have received a copy of the GNU General Public License |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3453 | 20 ## -*- texinfo -*- |
4885 | 21 ## @deftypefn {Function File} {} studentize (@var{x}, @var{dim}) |
3453 | 22 ## If @var{x} is a vector, subtract its mean and divide by its standard |
3200 | 23 ## deviation. |
24 ## | |
4885 | 25 ## If @var{x} is a matrix, do the above along the first non-singleton |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
26 ## dimension. If the optional argument @var{dim} is given then operate |
4885 | 27 ## along this dimension. |
3453 | 28 ## @end deftypefn |
3200 | 29 |
5428 | 30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 31 ## Description: Subtract mean and divide by standard deviation |
3200 | 32 |
4885 | 33 function t = studentize (x, dim) |
3426 | 34 |
4885 | 35 if (nargin != 1 && nargin != 2) |
6046 | 36 print_usage (); |
3200 | 37 endif |
38 | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
39 if (!ismatrix(x) || ischar(x)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
40 error ("studentize: X must be a numeric matrix or vector"); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
41 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
42 |
4885 | 43 nd = ndims (x); |
44 sz = size (x); | |
45 if (nargin != 2) | |
4886 | 46 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
47 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
48 if (isempty (dim)) |
4885 | 49 dim = 1; |
3200 | 50 endif |
51 else | |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
52 if (!(isscalar (dim) && dim == round (dim)) || |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
53 !(1 <= dim && dim <= nd)) |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
54 error ("studentize: DIM must be an integer and a valid dimension"); |
4885 | 55 endif |
56 endif | |
57 | |
4886 | 58 c = sz(dim); |
4885 | 59 idx = ones (1, nd); |
4886 | 60 idx(dim) = c; |
4885 | 61 t = x - repmat (mean (x, dim), idx); |
62 t = t ./ repmat (max (cat (dim, std(t, [], dim), ! any (t, dim)), [], dim), idx); | |
63 | |
64 endfunction |