Mercurial > hg > octave-nkf
comparison scripts/statistics/base/studentize.m @ 4885:28ab079d8f0e
[project @ 2004-04-30 04:21:33 by jwe]
author | jwe |
---|---|
date | Fri, 30 Apr 2004 04:21:33 +0000 |
parents | 22bd65326ec1 |
children | 54b076a24718 |
comparison
equal
deleted
inserted
replaced
4884:a9f67193e3a0 | 4885:28ab079d8f0e |
---|---|
16 ## along with Octave; see the file COPYING. If not, write to the Free | 16 ## along with Octave; see the file COPYING. If not, write to the Free |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | 17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
18 ## 02111-1307, USA. | 18 ## 02111-1307, USA. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {} studentize (@var{x}) | 21 ## @deftypefn {Function File} {} studentize (@var{x}, @var{dim}) |
22 ## If @var{x} is a vector, subtract its mean and divide by its standard | 22 ## If @var{x} is a vector, subtract its mean and divide by its standard |
23 ## deviation. | 23 ## deviation. |
24 ## | 24 ## |
25 ## If @var{x} is a matrix, do the above for each column. | 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. | |
26 ## @end deftypefn | 28 ## @end deftypefn |
27 | 29 |
28 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> | 30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
29 ## Description: Subtract mean and divide by standard deviation | 31 ## Description: Subtract mean and divide by standard deviation |
30 | 32 |
31 function t = studentize (x) | 33 function t = studentize (x, dim) |
32 | 34 |
33 if (nargin != 1) | 35 if (nargin != 1 && nargin != 2) |
34 usage ("studentize (x)"); | 36 usage ("studentize (x, dim)"); |
35 endif | 37 endif |
36 | 38 |
37 if isvector (x) | 39 nd = ndims (x); |
38 if (std (x) == 0) | 40 sz = size (x); |
39 t = zeros (size (x)); | 41 if (nargin != 2) |
40 else | 42 %% Find the first non-singleton dimension |
41 t = (x - mean (x)) / std (x); | 43 dim = 1; |
44 while (dim < nd + 1 && sz (dim) == 1) | |
45 dim = dim + 1; | |
46 endwhile | |
47 if (dim > nd) | |
48 dim = 1; | |
42 endif | 49 endif |
43 elseif ismatrix (x) | |
44 l = ones (rows (x), 1); | |
45 t = x - l * mean (x); | |
46 t = t ./ (l * max ([(std (t)); (! any (t))])); | |
47 else | 50 else |
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)) | |
48 error ("studentize: x must be a vector or a matrix"); | 58 error ("studentize: x must be a vector or a matrix"); |
49 endif | 59 endif |
50 | 60 |
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 | |
51 endfunction | 67 endfunction |