comparison scripts/statistics/base/std.m @ 4844:9f7ef92b50b0

[project @ 2004-04-02 17:26:53 by jwe]
author jwe
date Fri, 02 Apr 2004 17:26:54 +0000
parents f8dde1807dee
children c08cb1098afc
comparison
equal deleted inserted replaced
4843:7b4e76100964 4844:9f7ef92b50b0
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} {} std (@var{x}) 21 ## @deftypefn {Function File} {} std (@var{x})
22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt})
23 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}, @var{dim})
22 ## If @var{x} is a vector, compute the standard deviation of the elements 24 ## If @var{x} is a vector, compute the standard deviation of the elements
23 ## of @var{x}. 25 ## of @var{x}.
24 ## @iftex 26 ## @iftex
25 ## @tex 27 ## @tex
26 ## $$ 28 ## $$
36 ## @end group 38 ## @end group
37 ## @end example 39 ## @end example
38 ## @end ifinfo 40 ## @end ifinfo
39 ## If @var{x} is a matrix, compute the standard deviation for 41 ## If @var{x} is a matrix, compute the standard deviation for
40 ## each column and return them in a row vector. 42 ## each column and return them in a row vector.
43 ##
44 ## The argument @var{opt} determines the type of normalization to use. Valid values
45 ## are
46 ##
47 ## @table @asis
48 ## @item 0:
49 ## normalizes with N-1, provides the square root of best unbiased estimator of
50 ## the variance [default]
51 ## @item 1:
52 ## normalizes with N, this provides the square root of the second moment around
53 ## the mean
54 ## @end table
55 ##
56 ## The third argument @var{dim} determines the dimension along which the standard
57 ## deviation is calculated.
41 ## @end deftypefn 58 ## @end deftypefn
42 ## @seealso{mean and median} 59 ## @seealso{mean and median}
43 60
44 ## Author: jwe 61 ## Author: jwe
45 62
46 function retval = std (a) 63 function retval = std (a, opt, dim)
47 64
48 if (nargin != 1) 65 if (nargin < 1 || nargin > 3)
49 usage ("std (a)"); 66 usage ("std (a, opt, dim)");
67 endif
68 if nargin < 3
69 dim = min(find(size(a)>1));
70 if isempty(dim), dim=1; endif;
71 endif
72 if ((nargin < 2) || isempty(opt))
73 opt = 0;
50 endif 74 endif
51 75
52 nr = rows (a); 76 sz = size(a);
53 nc = columns (a); 77 if (sz (dim) == 1)
54 if (nc == 1 && nr == 1) 78 retval = zeros(sz);
55 retval = 0; 79 elseif (numel (a) > 0)
56 elseif (nc == 1 || nr == 1) 80 rng = ones (1, length (sz));
57 n = length (a); 81 rng (dim) = sz (dim);
58 retval = sqrt (sumsq (a - mean (a)) / (n - 1)); 82 if (opt == 0)
59 elseif (nr > 1 && nc > 0) 83 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1));
60 retval = sqrt (sumsq (a - ones (nr, 1) * mean (a)) / (nr - 1)); 84 else
85 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim));
86 endif
61 else 87 else
62 error ("std: invalid matrix argument"); 88 error ("std: invalid matrix argument");
63 endif 89 endif
64 90
65 endfunction 91 endfunction