function y = quantile(x,p)
% QUANTILE - calculate quantiles of the data x
%
% calculate quantiles of the data x
%  at fractions 0<=p<=1
%  y = quantile(x,p)

% Copyright (c) 1998 by Datatool
% Revision: 1.00

% This function comes from http://datatool.com/ and appears to have been 
% modified and separated from the rest of the datatool dvt toolbox without 
% comment.
% VERSION:
%	$Revision: 1.6 $
%	$Date: 2008/01/20 01:00:46 $
%	$Id: quantile.m,v 1.6 2008/01/20 01:00:46 duncombe Exp duncombe $

if size(x,1)==1
   x = x(:);
end

x = sort(x);
n = size(x,1);

%  standard fractions for this number of points
f = ((1:n)-0.5)/n;

index = p<min(f);
p(index) = min(f);
index = p>max(f);
p(index) = max(f);

%  use nearest neighbor interpolation
index = interp1(f,1:n,p,'nearest');
y = x(index,:);


