function p_inds = percentile_pick(data, p, which_end)

if ~exist('which_end','var')
    which_end = 'top';
end


N = size(data,1);
X = [ data, [1:N]' ];

Y = sortrows(X);
n = floor(p*N/100);

if strcmpi(which_end,'top')
    a_inds =  Y(1:n,2);
elseif strcmpi(which_end,'bottom')
    a_inds = Y(end-n:end,2);
end

    p_inds = zeros(N,1);
    p_inds(a_inds) = 1;
    p_inds = logical(p_inds);
    

end