Code to Calculate Modal Value

Zenerdiode

Active Member
Licensed User
I have a routine that tests a mechanical timer a number of times. I would like to display the Mean, Modal, Median, Maximum and Minimum times.

I sort the ArrayList before using the subs. I'm struggling with the code to calculate the modal value. I appreciate in my dataset there may be (rarely) more than one modal value.

B4X:
#Region StatsCalculators
Sub CalcMean
   For i=0 To ArrayList1.Count-1
      x=x+ArrayList1.Item(i)
   Next
   Return Format(x/ArrayList1.Count,"F2")
End Sub

Sub CalcMode
   Return "Uniform Dist." 'Unless there is a genuine Modal value.
End Sub

Sub CalcMed
   If ArrayList1.Count Mod 2=1 Then
      Return ArrayList1.Item(((ArrayList1.Count+1)/2)-1)
   Else
      Return Format((ArrayList1.Item((ArrayList1.Count/2)-1)+ArrayList1.Item(ArrayList1.Count/2))/2,"F2")
   End If
End Sub

Sub CalcMax
   Return ArrayList1.Item(ArrayList1.Count-1)
End Sub

Sub CalcMin
   Return ArrayList1.Item(0)
End Sub
#End Region
 

Zenerdiode

Active Member
Licensed User
Thanks klaus,

That's exactly what I was wanting. :)

*EDIT* - Not quite. If the dataset array does not have a modal value (i.e. a Uniform Distribution) the sub errors. Also, a dataset of, say, (2,3,3) reports '1*2' rather than '2*3'
 
Last edited:
Top