source: issm/trunk/src/m/miscellaneous/prctile_issm.m@ 23189

Last change on this file since 23189 was 23189, checked in by Mathieu Morlighem, 7 years ago

merged trunk-jpl and trunk for revision 23187

File size: 1.4 KB
RevLine 
[14196]1%
2% wrapper for prctile to avoid using the matlab statistics toolbox.
3%
4function [y]=prctile_issm(x,p,dim)
5
6 try
7 y=prctile(argin{:});
8
9 catch me
10 if length(size(x)) > 2
11 error('Number of dimensions %d not implemented.',length(size(x)));
12 end
13 if ~exist('dim','var')
14 dim=0;
15 for i=1:length(size(x))
16 if ~dim && size(x,i)>1
17 dim=i;
18 end
19 end
20 if ~dim
21 dim=1;
22 end
23 end
24
25 psize=size(p);
26 if size(p,2)>1
[18301]27 p=transpose(p);
[14196]28 end
29
30 xsize=size(x);
31 if dim==2
[18301]32 x=transpose(x);
[14196]33 end
34
35% check for any NaN in any columns
36
[15396]37 if ~any(any((isnan(x))))
[14196]38 x=sort(x,1);
39 n=size(x,1);
40
41% branch based on number of elements
42
43 if n>1
44
45% set up percent values and interpolate
46
[18301]47 xi=transpose(100.*([1:n]-0.5)/n);
[14196]48 y=interp1q(xi,x,p);
49
50% fill in high and low values
51 y(p<xi(1),:)=repmat(x(1,:),nnz(p<xi(1)),1);
52 y(p>xi(n),:)=repmat(x(n,:),nnz(p>xi(n)),1);
53
54% if one value, just copy it
55
56 elseif n==1
57 y=repmat(x(1,:),length(p),1);
58
59% if no values, use NaN
60
61 else
62 y=repmat(NaN,size(p,1),size(x,2));
63 end
64
65 else
66
67% must loop over columns, since number of elements could be different
68
69 y=zeros(size(p,1),size(x,2));
70 for j=1:size(x,2)
71
72% remove any NaN and recursively call column
73
74 y(:,j)=prctile_issm(x(~isnan(x(:,j)),j),p);
75 end
76 end
77
78 if (min(xsize)==1 && xsize(dim)>1 && psize(2)>1) || ...
79 (min(xsize)> 1 && dim==2)
[18301]80 y=transpose(y);
[14196]81 end
82 end
83end
Note: See TracBrowser for help on using the repository browser.