]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/sem.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / sem.m
1 function [SE,M]=sem(x,DIM, W)
2 % SEM calculates the standard error of the mean
3
4 % [SE,M] = SEM(x [, DIM [,W]])
5 %   calculates the standard error (SE) in dimension DIM
6 %   the default DIM is the first non-single dimension
7 %   M returns the mean. 
8 %   Can deal with complex data, too. 
9 %
10 % DIM   dimension
11 %       1: SEM of columns
12 %       2: SEM of rows
13 %       N: SEM of  N-th dimension 
14 %       default or []: first DIMENSION, with more than 1 element
15 % W     weights to compute weighted mean and s.d. (default: [])
16 %       if W=[], all weights are 1. 
17 %       number of elements in W must match size(x,DIM) 
18 %
19 % features:
20 % - can deal with NaN's (missing values)
21 % - weighting of data 
22 % - dimension argument 
23 % - compatible to Matlab and Octave
24 %
25 % see also: SUMSKIPNAN, MEAN, VAR, STD
26
27 %    This program is free software; you can redistribute it and/or modify
28 %    it under the terms of the GNU General Public License as published by
29 %    the Free Software Foundation; either version 3 of the License, or
30 %    (at your option) any later version.
31 %
32 %    This program is distributed in the hope that it will be useful,
33 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
34 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35 %    GNU General Public License for more details.
36 %
37 %    You should have received a copy of the GNU General Public License
38 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
39
40 %       Copyright (C) 2000-2003,2008,2009 by Alois Schloegl <alois.schloegl@gmail.com>  
41 %       $Id: sem.m 8223 2011-04-20 09:16:06Z schloegl $
42 %       This function is part of the NaN-toolbox
43 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
44
45
46 if nargin>2,
47         [S,N,SSQ] = sumskipnan(x,DIM,W);
48 elseif nargin>1,
49         [S,N,SSQ] = sumskipnan(x,DIM);
50 else    
51         [S,N,SSQ] = sumskipnan(x);
52 end
53
54 M  = S./N;
55 SE = (SSQ.*N - real(S).^2 - imag(S).^2)./(N.*N.*(N-1)); 
56 SE(SE<=0) = 0;  % prevent negative value caused by round-off error  
57 SE = sqrt(real(SE));
58