Moving average

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Moving average

Post by geo » Sat Oct 03, 2020 8:15 pm

Hello
I want to create a moving average of the last three values in Col1 and put it in Col2
In Excel it looks like the screenshot below.
I cant't figure out how to do it in LC
There will be a few thousand lines of data so I don't know if I should work with arrays or fields
Any hints?
Screenshot 2020-10-03 at 21.07.47.png

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Moving average

Post by dunbarx » Sat Oct 03, 2020 9:15 pm

Hi.

So much fun.

On a new card, make a table field, and put any small numbers into item 1 of the first 20 lines. Maybe in a button somewhere:

Code: Select all

on mouseUp
   set the itemDel to tab
   repeat with y = 1 to 20
      put random(20) into item 1 of line y of fld 1
   end repeat
end mouseUp
Now in another button script:

Code: Select all

on mouseUp
   get fld 1
   set the itemDel to tab
   set the numberFormat to "#.00"
   repeat with y = 3 to 20
      put (item 1 of line (y-2) of it + item 1 of line (y-1) of it + item 1 of line y of it) / 3 into item 2 of line y of fld 1
   end repeat
end mouseUp
Could you do this other ways? Yes.

Craig

geo
Posts: 31
Joined: Sun Aug 04, 2019 7:28 pm

Re: Moving average

Post by geo » Sun Oct 04, 2020 8:34 am

Thanks Craig, that put me on the right track

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Moving average

Post by dunbarx » Sun Oct 04, 2020 4:31 pm

The thing about this, really, is to be comfortable using "chunks", items, lines, words, chars, etc.

In a way this method is clunky, using the "format" of the dataSet to massage the dataSet. And if you are doing this sort of thing with a lot of data, I would keep everything in a variable, and load the table field all at once in the last line. I should have done that anyway, just to do it right the first time.

Code: Select all

...
repeat with y = 3 to 20
   put  (item 1 of line (y-2) of it + item 1 of line (y-1) of it + item 1 of line y of it) / 3 into item 2 of line y of it
end repeat
put it into fld 1...
Craig

Post Reply