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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, 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) |
|
42 %% Find the first non-singleton dimension |
|
43 dim = 1; |
|
44 while (dim < nd + 1 && sz (dim) == 1) |
|
45 dim = dim + 1; |
|
46 endwhile |
|
47 if (dim > nd) |
|
48 dim = 1; |
3200
|
49 endif |
|
50 else |
4885
|
51 if (! (isscalar (dim) && dim == round (dim)) && dim > 0 && |
|
52 dim < (nd + 1)) |
|
53 error ("studentize: dim must be an integer and valid dimension"); |
|
54 endif |
|
55 endif |
|
56 |
|
57 if (! ismatrix (x)) |
3458
|
58 error ("studentize: x must be a vector or a matrix"); |
3200
|
59 endif |
|
60 |
4885
|
61 c = sz (dim); |
|
62 idx = ones (1, nd); |
|
63 idx (dim) = c; |
|
64 t = x - repmat (mean (x, dim), idx); |
|
65 t = t ./ repmat (max (cat (dim, std(t, [], dim), ! any (t, dim)), [], dim), idx); |
|
66 |
|
67 endfunction |