MasterACE14 Posted June 1, 2011 Share Posted June 1, 2011 Good Evening, I've created a function to get the Range of an array (Highest Value - Lowest Value). Now I'm trying to use this to find the Range of the Upper Quartile(Q3) and the Lower Quartile(Q1) but am not sure as to what is the best/most efficient way of doing this? Should I find the middle value of the array, and then create 2 new arrays, the top half and bottom half, then use the Range function on each of them to get Q3 and Q1 or is there a better way to go about doing this? $x = array(); sort($x); // calculate median (middle number/value) $count = count($x); $middleval = floor(($count-1)/2); if($count % 2) { $median = $x[$middleval]; } else { $low = $x[$middleval]; $high = $x[$middleval+1]; $median = (($low+$high)/2); } // calculate range $min = min($x); $max = max($x); return $range = $max - $min; any help/suggestions are appreciated! Thank you. Kind Regards, Ace Link to comment https://forums.phpfreaks.com/topic/238068-finding-the-interquartile-range-iqr/ Share on other sites More sharing options...
requinix Posted June 1, 2011 Share Posted June 1, 2011 You were able to figure the median out fairly easily, right? Why not apply that same logic to find the first and third quartiles? Link to comment https://forums.phpfreaks.com/topic/238068-finding-the-interquartile-range-iqr/#findComment-1223380 Share on other sites More sharing options...
MasterACE14 Posted June 1, 2011 Author Share Posted June 1, 2011 You were able to figure the median out fairly easily, right? Why not apply that same logic to find the first and third quartiles? Got it! I was over thinking it. Thanks! $x = array(7,3,4,6,4,2,7); $median = 4; $q1 = $median - min($x); $q3 = max($x) - $median; $iqr = $q3 - $q1; Link to comment https://forums.phpfreaks.com/topic/238068-finding-the-interquartile-range-iqr/#findComment-1223393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.