1014
|
1 # Copyright (C) 1993, 1994, 1995 John W. Eaton |
245
|
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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # 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 |
1315
|
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
245
|
18 |
4
|
19 function retval = std (a) |
|
20 |
|
21 # usage: std (a) |
|
22 # |
|
23 # For vector arguments, std returns the standard deviation of the |
|
24 # values. For matrix arguments, std returns a row vector containing |
|
25 # the standard deviation for each column. |
|
26 # |
|
27 # See also: mean, median |
|
28 |
|
29 if (nargin != 1) |
904
|
30 usage ("std (a)"); |
4
|
31 endif |
|
32 |
|
33 nr = rows (a); |
|
34 nc = columns (a); |
|
35 if (nc == 1 && nr == 1) |
|
36 retval = 0; |
|
37 elseif (nc == 1 || nr == 1) |
|
38 tmp = sum (a); |
|
39 n = length (a); |
|
40 retval = sqrt ((n * sumsq (a) - tmp .* tmp) / (n * (n - 1))); |
|
41 elseif (nr > 1 && nc > 0) |
|
42 tmp = sum (a); |
|
43 retval = sqrt ((nr * sumsq (a) - tmp .* tmp) / (nr * (nr - 1))); |
|
44 else |
|
45 error ("mean: invalid matrix argument"); |
|
46 endif |
|
47 |
|
48 endfunction |