I have a large array of doubles and I need to calculate the 75th and 90th percentile values for the array. What\'s the most efficient way to do this via a function?
Working off of Mark's function above, I believe the function should actually be:
function get_percentile($percentile, $array) {
sort($array);
$index = (($percentile/100) * (count($array))-1;
if (floor($index) == $index) {
return $array[$index];
}
else {
return ($array[floor($index)] + $array[ceiling($index)])/2;
}
}
I think there are three things that needed to be corrected:
count by one in order to avoid an out-of-range index (mentioned above)index is an integer, then you should be able to just return the index. You only need to average values when the index is not an integer.floor and ceiling to get the indices to average