Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Mean, Median, Mode Functions


  • Please log in to reply
1 reply to this topic
Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
;Returns Average values in comma delimited list

Mean(List)

{

	Loop, Parse, List , `,

	{

		Total += %A_LoopField%

		D = %A_Index%

	}

	R := Total/D

	Return R

}
;Median fucntion

;Returns Median in a set of numbers

;list must be comma delimited

Median(List)

{

	Sort, List, N D,  ; Sort numerically, use comma as delimiter.



	;Create Array

	StringSplit, Set, List, `,

;Figure if odd or even

	R := Set0 / 2

	StringSplit, B, R, .

	StringLeft, C, B2, 1

	;Even

	If C = 0

	{

		pt1 := B1 + 1

		Med := (Set%B1% + Set%pt1%) / 2

	}

	;Odd

	Else

	{

		Med := Ceil(R)

		Med := Set%Med%	

	}

	Return Med

}
;Mode funtion

;Returns the mode from a list of numbers

;List must be comma delimited

Mode(List)

{

	StringSplit, Cont, List, `,

	Loop, %Cont0%

	{

		i := A_Index

		C := Cont%i%

		If ModeArr%C% =

			ModeArr%C% = 1

		Else 

		{

			Amt := ModeArr%C%

			ModeArr%C% := Amt + 1

		}

	}

	Loop %i%

	{

		LMC = %CMC%

		CMC := ModeArr%A_Index%

		If CMC > %LMC%

			Mode = %A_Index%

	}

	Return Mode

}

my lame sig :)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I've always liked "median" as a statistic generally superior to "average". Thanks for posting these ready-to-run functions.