It does seem a bit of a limitation, but we can make up our own function.
We know that with an odd number of items of length n, the median is the item in position (n+1)/2.
Even numbers get a bit trickier, since we want a number halfway between the central two numbers. That is, ((n/2) + ((n+1)/2) / 2)
So what does this look like in Arcade?
First, we need to know if it's even or odd. That's easy enough, just use Count against your list and modulo divide by 2.
Second, we need to Sort the list. When using the Sort function without specifying a custom function will default to ordering your list items in ascending order.
Once sorted, we use one of the two formulas above depending on even or odd, then we return the value.
Final note: list indexes are 0-based, so we have to actually decrement the index we're returning.
Here's what it might look like with a sample input.
// Create a function to check even or odd
function isOdd(value){
return Boolean(value % 2);
}
// Create an array, populate it with random numbers from 1-100
var some_list = [];
for (var x = 0; x < 100; x++){
Push(some_list, Random()*100)
}
// Sort list
var ordered = Sort(some_list)
// Get count of items
var c = Count(ordered)
// Check if even or odd return appropriate value
if (isOdd(c)){
Console('Odd number of items. Returning central item')
return ordered[((c+1)/2)-1]
} else {
Console('Even number of items. Averaging central two')
var a = ordered[(c/2)-1]
var b = ordered[(c/2)]
return (a+b)/2
}
Just to see that it works, here are the outputs with known "correct" values.
- Josh Carlson
Kendall County GIS