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