3200
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
|
2 ## |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
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. |
|
19 |
3367
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} std (@var{x}) |
4844
|
22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}) |
|
23 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}, @var{dim}) |
3367
|
24 ## If @var{x} is a vector, compute the standard deviation of the elements |
|
25 ## of @var{x}. |
|
26 ## @iftex |
|
27 ## @tex |
|
28 ## $$ |
|
29 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x}) \over N - 1}} |
|
30 ## $$ |
|
31 ## @end tex |
|
32 ## @end iftex |
|
33 ## @ifinfo |
3426
|
34 ## |
3367
|
35 ## @example |
|
36 ## @group |
|
37 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1)) |
|
38 ## @end group |
|
39 ## @end example |
|
40 ## @end ifinfo |
|
41 ## If @var{x} is a matrix, compute the standard deviation for |
|
42 ## each column and return them in a row vector. |
4844
|
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. |
3367
|
58 ## @end deftypefn |
3408
|
59 ## @seealso{mean and median} |
3200
|
60 |
|
61 ## Author: jwe |
|
62 |
4844
|
63 function retval = std (a, opt, dim) |
3200
|
64 |
4844
|
65 if (nargin < 1 || nargin > 3) |
|
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; |
3200
|
74 endif |
|
75 |
4844
|
76 sz = size(a); |
|
77 if (sz (dim) == 1) |
|
78 retval = zeros(sz); |
|
79 elseif (numel (a) > 0) |
|
80 rng = ones (1, length (sz)); |
|
81 rng (dim) = sz (dim); |
|
82 if (opt == 0) |
|
83 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1)); |
|
84 else |
|
85 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim)); |
|
86 endif |
3200
|
87 else |
|
88 error ("std: invalid matrix argument"); |
|
89 endif |
|
90 |
|
91 endfunction |