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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3200
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3200
|
18 |
3367
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} std (@var{x}) |
4844
|
21 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}) |
|
22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}, @var{dim}) |
3367
|
23 ## If @var{x} is a vector, compute the standard deviation of the elements |
|
24 ## of @var{x}. |
|
25 ## @iftex |
|
26 ## @tex |
|
27 ## $$ |
6754
|
28 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367
|
29 ## $$ |
6754
|
30 ## where $\bar{x}$ is the mean value of $x$. |
3367
|
31 ## @end tex |
|
32 ## @end iftex |
6754
|
33 ## @ifnottex |
3426
|
34 ## |
3367
|
35 ## @example |
|
36 ## @group |
|
37 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1)) |
|
38 ## @end group |
|
39 ## @end example |
6754
|
40 ## @end ifnottex |
3367
|
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: |
6754
|
49 ## normalizes with @math{N-1}, provides the square root of best unbiased estimator of |
4844
|
50 ## the variance [default] |
|
51 ## @item 1: |
6754
|
52 ## normalizes with @math{N}, this provides the square root of the second moment around |
4844
|
53 ## the mean |
|
54 ## @end table |
|
55 ## |
|
56 ## The third argument @var{dim} determines the dimension along which the standard |
|
57 ## deviation is calculated. |
5642
|
58 ## @seealso{mean, median} |
3367
|
59 ## @end deftypefn |
3200
|
60 |
|
61 ## Author: jwe |
|
62 |
4844
|
63 function retval = std (a, opt, dim) |
3200
|
64 |
4844
|
65 if (nargin < 1 || nargin > 3) |
6046
|
66 print_usage (); |
4844
|
67 endif |
|
68 if nargin < 3 |
6024
|
69 dim = find (size (a) > 1, 1); |
4844
|
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 |