3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 |
|
26 ## dimension. If the optional argument @var{dim} is given then operate |
|
27 ## along this dimension. |
3453
|
28 ## @end deftypefn |
3200
|
29 |
3456
|
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
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) |
|
36 usage ("studentize (x, dim)"); |
3200
|
37 endif |
|
38 |
4885
|
39 nd = ndims (x); |
|
40 sz = size (x); |
|
41 if (nargin != 2) |
4886
|
42 ## Find the first non-singleton dimension. |
4885
|
43 dim = 1; |
4886
|
44 while (dim < nd + 1 && sz(dim) == 1) |
4885
|
45 dim = dim + 1; |
|
46 endwhile |
|
47 if (dim > nd) |
|
48 dim = 1; |
3200
|
49 endif |
|
50 else |
4886
|
51 if (! (isscalar (dim) && dim == round (dim)) |
|
52 && dim > 0 |
|
53 && dim < (nd + 1)) |
4885
|
54 error ("studentize: dim must be an integer and valid dimension"); |
|
55 endif |
|
56 endif |
|
57 |
|
58 if (! ismatrix (x)) |
3458
|
59 error ("studentize: x must be a vector or a matrix"); |
3200
|
60 endif |
|
61 |
4886
|
62 c = sz(dim); |
4885
|
63 idx = ones (1, nd); |
4886
|
64 idx(dim) = c; |
4885
|
65 t = x - repmat (mean (x, dim), idx); |
|
66 t = t ./ repmat (max (cat (dim, std(t, [], dim), ! any (t, dim)), [], dim), idx); |
|
67 |
|
68 endfunction |