]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/ecdf.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / ecdf.m
1 function [F,X]=ecdf(h,Y)
2 % ECDF empirical cumulative function  
3 %  NaN's are considered Missing values and are ignored. 
4 %
5 %  [F,X] = ecdf(Y)
6 %       calculates empirical cumulative distribution functions (i.e Kaplan-Meier estimate)
7 %  ecdf(Y)
8 %  ecdf(gca,Y)
9 %       without output arguments plots the empirical cdf, in axis gca. 
10 %
11 % Y     input data
12 %       must be a vector or matrix, in case Y is a matrix, the ecdf for every column is computed. 
13 %
14 % see also: HISTO2, HISTO3, PERCENTILE, QUANTILE
15
16
17 %       $Id: ecdf.m 8223 2011-04-20 09:16:06Z schloegl $
18 %       Copyright (C) 2009,2010 by Alois Schloegl <alois.schloegl@gmail.com>    
19 %       This function is part of the NaN-toolbox
20 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
21
22 %    This program is free software; you can redistribute it and/or modify
23 %    it under the terms of the GNU General Public License as published by
24 %    the Free Software Foundation; either version 3 of the License, or
25 %    (at your option) any later version.
26 %
27 %    This program is distributed in the hope that it will be useful,
28 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
29 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 %    GNU General Public License for more details.
31 %
32 %    You should have received a copy of the GNU General Public License
33 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
34
35 if ~isscalar(h) || ~ishandle(h) || isstruct(h),
36         Y = h; 
37         h = []; 
38 end;    
39
40 DIM = [];
41
42         SW = isstruct(Y);
43         if SW, SW = isfield(Y,'datatype'); end;
44         if SW, SW = strcmp(Y.datatype,'HISTOGRAM'); end;
45         if SW,                 
46                 [yr,yc]=size(Y.H);
47                 if ~isfield(Y,'N');
48                         Y.N = sum(Y.H,1);
49                 end;
50                 f = [zeros(1,yc);cumsum(Y.H,1)];
51                 for k=1:yc,
52                         f(:,k)=f(:,k)/Y.N(k); 
53                 end;            
54                 t = [Y.X(1,:);Y.X]; 
55
56         elseif isnumeric(Y),
57                 sz = size(Y);
58                 if isempty(DIM),
59                         DIM = min(find(sz>1));
60                         if isempty(DIM), DIM = 1; end;
61                 end;
62                 if DIM==2, Y=Y.'; DIM = 1; end;         
63                 
64                 t = sort(Y,1); 
65                 t = [t(1,:);t];         
66                 N = sum(~isnan(Y),1); 
67                 f = zeros(size(Y,1)+1,size(Y,2));
68                 for k=1:size(Y,2),
69                         f(:,k)=[0:size(Y,1)]'/N(k); 
70                 end;            
71         end; 
72         
73         if nargout<1, 
74                 if  ~isempty(h), axes(h); end; 
75                 stairs(t,f);
76         else 
77                 F = f;
78                 X = t;  
79         end;                    
80