getprctileW.m (1199B)
1 function [xprc,indx]=getprctileW(x,weight,prctile); 2 3 % getprctileW.m function [xprc,indx]=getprctile(x,weight,prctile); 4 % 5 % Computes the requested percentiles (e.g. 50 or 25) of the vector x. 6 % Allows each element of x to have an associated weight, corresponding to 7 % the true number of people/obs that the element represents. So, we compute 8 % the percentiles from the cumulative sum of the weights... 9 % 10 % xprc are the percentile values 11 % indx points to the observation so that xprc=x(indx) 12 13 N=length(x); 14 [xsort,blah]=sort(x); 15 w=100*cumsum(weight(blah)/sum(weight)); % Now w=cumulative distribution for sorted data 16 17 xprc=zeros(length(prctile),1); 18 indx=zeros(length(prctile),1); 19 for j=1:length(prctile); 20 iless=find(w<=prctile(j)); 21 imore=find(w>=prctile(j)); 22 miless=max(iless); 23 mimore=min(imore); 24 if isempty(miless); miless=1; end; 25 if isempty(mimore); mimore=N; end; 26 xprc(j)=xsort(mimore); 27 indx(j)=blah(mimore); 28 end; 29 30 % if miless==mimore; 31 % xprc(j)=xsort(miless); 32 % else; 33 % slope=(xsort(mimore)-xsort(miless))/(w(mimore)-w(miless)); 34 % xprc(j)=xsort(miless)+slope*(prctile(j)-w(miless)); 35 % disp 'just checking keyboard...'; keyboard 36 % end;