7017
|
1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007 |
|
2 ## 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); |
4844
|
71 if isempty(dim), dim=1; endif; |
|
72 endif |
|
73 if ((nargin < 2) || isempty(opt)) |
|
74 opt = 0; |
3200
|
75 endif |
|
76 |
4844
|
77 sz = size(a); |
|
78 if (sz (dim) == 1) |
|
79 retval = zeros(sz); |
|
80 elseif (numel (a) > 0) |
|
81 rng = ones (1, length (sz)); |
|
82 rng (dim) = sz (dim); |
|
83 if (opt == 0) |
|
84 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1)); |
|
85 else |
|
86 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim)); |
|
87 endif |
3200
|
88 else |
|
89 error ("std: invalid matrix argument"); |
|
90 endif |
|
91 |
|
92 endfunction |