function final_percentile=myperctl(values,frequency,percentilenum)
[frequency_new,k]=gsort(frequency,'g','i'); //sort the population in increasing order and return the indices corresponding to the sort order in "k"
values_new=values(k); //reorder the frequency vector using k
[numentries,temp]=size(values_new);
i=1;
serialized_values=[];
for j=1:numentries
    for k = 1:frequency_new(j)
        serialized_values = [serialized_values values_new(j)]; 
        i = i +1 ;
    end;
end

final_percentile=perctl(serialized_values, percentilenum);
endfunction
