Moving Average of a Moving Average Of an Array Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Woolley89
Posts: 8
Joined: 17 Nov 2020, 17:13

Moving Average of a Moving Average Of an Array

17 Nov 2020, 17:45

I've been scratching my head on this for a while, and haven't found out how to have a moving average of a moving average of an array

Example:

Code: Select all

array_source := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
first_average := moving_average(array_source, 2)
last_average := moving_average(first_average, 3)

arrayStr(array) {
	Str := ""
	For Index, Value In array
	   Str .= ", " . Value
	return "[ " LTrim(Str, ", ") " ]" 
}

MsgBox, % arrayStr(array_source) "`n" arrayStr(first_average) "`n" arrayStr(last_average)

;MsgBox response should be:
; [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
; [ na, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5 ]
; [ na, na, na, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5 ]
This is a screenshot of a excel sheet example I made:
Screenshot (814).png
Screenshot (814).png (9.73 KiB) Viewed 36 times
User avatar
boiler
Posts: 6631
Joined: 21 Dec 2014, 02:44

Re: Moving Average of a Moving Average Of an Array  Topic is solved

17 Nov 2020, 19:30

Code: Select all

array_source := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
first_average := moving_average(array_source, 2)
last_average := moving_average(first_average, 3)
MsgBox, % arrayStr(array_source) "`n" arrayStr(first_average) "`n" arrayStr(last_average)
return

arrayStr(array) {
	Str := ""
	For Index, Value In array
	   Str .= ", " . Value
	return "[ " LTrim(Str, ", ") " ]" 
}

moving_average(a, n) {
	b := [], c := []
	loop {
		if (a[A_Index] = "na")
			b.Push("na")
		else
			break
	}
	for k, v in a
		if (v != "na")
			c.Push(v)
	loop, % n - 1
		b.Push("na")
	loop, % c.Count() - n + 1 {
		sum := 0
		i := A_Index
		loop, % n
			sum += c[i + A_Index - 1]
		b.Push(Round(sum / n, 1))
	}
	return b
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], isuckatahk, jly and 46 guests