tinarg
|
  |
| Joined: 18 Jun 2010 |
| Total Posts: 4925 |
|
| |
|
ohno1112
|
  |
| Joined: 23 Mar 2013 |
| Total Posts: 833 |
|
|
| 07 Jul 2014 01:31 PM |
a = Number1 + number2 + number3 median = a/3 |
|
|
| Report Abuse |
|
|
domorox17
|
  |
| Joined: 06 Mar 2012 |
| Total Posts: 1710 |
|
|
| 07 Jul 2014 01:34 PM |
| That is the mean, or average... |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2014 01:40 PM |
That would be the Arithmetic Mean.
OP wants the median.
1,2,3,4,5
Median = 3
2, 7 , 11 , 17, 23 , 29, 32
Median = 17
It is the middle result in a group of raw data in ascending order. |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2014 01:51 PM |
For three numbers, you could use this:
local a, b, c = 12, 16, -167 local median = math.min(math.max(a, b), math.max(b, c), math.max(a, c)) print( median )
If you want more, I suggest using a table and sorting it. |
|
|
| Report Abuse |
|
|
Conmiro
|
  |
| Joined: 13 Oct 2008 |
| Total Posts: 3393 |
|
|
| 07 Jul 2014 02:08 PM |
-- Get the median of a table. function stats.median( t ) local temp={}
-- deep copy table so that when we sort it, the original is unchanged -- also weed out any non numbers for k,v in pairs(t) do if type(v) == 'number' then table.insert( temp, v ) end end
table.sort( temp )
-- If we have an even number of table elements or odd. if math.fmod(#temp,2) == 0 then -- return mean value of middle two elements return ( temp[#temp/2] + temp[(#temp/2)+1] ) / 2 else -- return middle element return temp[math.ceil(#temp/2)] end end |
|
|
| Report Abuse |
|
|
Conmiro
|
  |
| Joined: 13 Oct 2008 |
| Total Posts: 3393 |
|
|
| 07 Jul 2014 02:10 PM |
-- Get the median of a table. function statsmedian( t ) local temp={}
-- deep copy table so that when we sort it, the original is unchanged -- also weed out any non numbers for k,v in pairs(t) do if type(v) == 'number' then table.insert( temp, v ) end end
table.sort( temp )
-- If we have an even number of table elements or odd. if math.fmod(#temp,2) == 0 then -- return mean value of middle two elements return ( temp[#temp/2] + temp[(#temp/2)+1] ) / 2 else -- return middle element return temp[math.ceil(#temp/2)] end end
print(statsmedian({4,5,2,3,4})) |
|
|
| Report Abuse |
|
|