8920
|
1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007, 2008, |
|
2 ## 2009 John W. Eaton |
3200
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3200
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
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 ## $$ |
6754
|
29 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367
|
30 ## $$ |
6754
|
31 ## where $\bar{x}$ is the mean value of $x$. |
3367
|
32 ## @end tex |
|
33 ## @end iftex |
6754
|
34 ## @ifnottex |
3426
|
35 ## |
3367
|
36 ## @example |
|
37 ## @group |
|
38 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1)) |
|
39 ## @end group |
|
40 ## @end example |
6754
|
41 ## @end ifnottex |
3367
|
42 ## If @var{x} is a matrix, compute the standard deviation for |
|
43 ## each column and return them in a row vector. |
4844
|
44 ## |
|
45 ## The argument @var{opt} determines the type of normalization to use. Valid values |
|
46 ## are |
|
47 ## |
|
48 ## @table @asis |
|
49 ## @item 0: |
6754
|
50 ## normalizes with @math{N-1}, provides the square root of best unbiased estimator of |
4844
|
51 ## the variance [default] |
|
52 ## @item 1: |
6754
|
53 ## normalizes with @math{N}, this provides the square root of the second moment around |
4844
|
54 ## the mean |
|
55 ## @end table |
|
56 ## |
|
57 ## The third argument @var{dim} determines the dimension along which the standard |
|
58 ## deviation is calculated. |
5642
|
59 ## @seealso{mean, median} |
3367
|
60 ## @end deftypefn |
3200
|
61 |
|
62 ## Author: jwe |
|
63 |
4844
|
64 function retval = std (a, opt, dim) |
3200
|
65 |
4844
|
66 if (nargin < 1 || nargin > 3) |
6046
|
67 print_usage (); |
4844
|
68 endif |
|
69 if nargin < 3 |
6024
|
70 dim = find (size (a) > 1, 1); |
8507
|
71 if (isempty (dim)) |
|
72 dim = 1; |
|
73 endif |
4844
|
74 endif |
8507
|
75 if (nargin < 2 || isempty (opt)) |
4844
|
76 opt = 0; |
3200
|
77 endif |
|
78 |
4844
|
79 sz = size(a); |
|
80 if (sz (dim) == 1) |
|
81 retval = zeros(sz); |
|
82 elseif (numel (a) > 0) |
|
83 rng = ones (1, length (sz)); |
|
84 rng (dim) = sz (dim); |
|
85 if (opt == 0) |
|
86 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1)); |
|
87 else |
|
88 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim)); |
|
89 endif |
3200
|
90 else |
|
91 error ("std: invalid matrix argument"); |
|
92 endif |
|
93 |
|
94 endfunction |
7411
|
95 |
|
96 %!test |
|
97 %! x = ones (10, 2); |
|
98 %! y = [1, 3]; |
|
99 %! assert(std (x) == [0, 0] && abs (std (y) - sqrt (2)) < sqrt (eps)); |
|
100 |
|
101 %!error std (); |
|
102 |
|
103 %!error std (1, 2, 3, 4); |
|
104 |