function f = percentile(v,frac,bApprox);
%function f = percentile(v,frac,bApprox);
%
%bApprox is default 0
%returns a  such that frac part of v is less than f

v = v(:);
if(nargin < 3), bApprox = 0;, end
if(bApprox)
  nnecc = min(length(v),max(bApprox,1000));
  % p = randperm(length(v));
  p = find(rand(length(v),1)<(nnecc/length(v)));
  [s,si] = sort(v(p));
else
  [s,si] = sort(v);
end


u = ceil(frac*length(si));
f = s(u);