Thanks
I had to take a different approach, as I have a number of different categories of things all in the same table, so I copied each data type into a separate array and wrote the following function (Method)
It is called by passing the array as follows: Dim medianOfArray As Double = CalculateMedian(ArrrayToBePassed)
It also takes into account the special cases where there are nil, only one, or two values that are being calculated.
// CalculateMedian(ArrayToAnalyse() As Double)
Dim medianValue As Double
// first sort the array
ArrayToAnalyse.sort
// Get the number of entries in the array - one more than the index for ubound, as the array starts with a zero
Dim n As Integer = ArrayToAnalyse.Ubound + 1
// if n > -1 array has values
if n > 0 then
// Display array for testing purposes
'for i As integer = 0 to ArrayToAnalyse.Ubound
'MsgBox("Aarray value = " + Str(ArrayToAnalyse(i)))
'next
if n mod 2 = 0 then
MsgBox(“Array has even number of entries…” + Str(n))
// median is the average of the two middle values
if n = 2 then // special case
'msgBox(" The two values to average are " + Str(ArrayToAnalyse(0) ) + " and " + Str(ArrayToAnalyse(1) ))
medianValue = (ArrayToAnalyse(0) + ArrayToAnalyse(1) ) / 2
else // more than two entries
'msgBox(" The two central values to average are " + Str(ArrayToAnalyse(n/2 - 1) ) + " and " + Str(ArrayToAnalyse(n/2) ))
medianValue = (ArrayToAnalyse(n/2 - 1) + ArrayToAnalyse(n/2))/2
end if
else // an odd number of entries
'MsgBox(“Array has an odd number of entries…” + Str(n))
if n = 1 then /// special case
medianValue = ArrayToAnalyse(0)
else
medianValue = ArrayToAnalyse((n-1)/2)
end if
end
'MsgBox("Median value to be returned is " + Str(medianValue))
return medianValue
else
return 0.0 // default to zero if cannot calculate the median value
end if
================
I hope this helps someone else looking fo this feature. Pity it isn’t included in the array type viz: Array.median
There should be a way to extend this to give back the median, interquartile range and identify outliers…
Will have to think about it.
Phil