Page 1 of 1

Moving average

Posted: Sat Oct 03, 2020 8:15 pm
by geo
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

Re: Moving average

Posted: Sat Oct 03, 2020 9:15 pm
by dunbarx
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

Re: Moving average

Posted: Sun Oct 04, 2020 8:34 am
by geo
Thanks Craig, that put me on the right track

Re: Moving average

Posted: Sun Oct 04, 2020 4:31 pm
by dunbarx
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