Moving average in j is nicely done with \ e.g.
data =. ". ;._2 ] 0 : 0
0 1.00
1 1.20
2 1.15
3 1.25
4 2.90
5 2.70
6 2.75
)
ave =. +/%#
ave data
3 1.85
3 <\ data
+------+------+------+------+------+
|0 1|1 1.2|2 1.15|3 1.25|4 2.9|
|1 1.2|2 1.15|3 1.25|4 2.9|5 2.7|
|2 1.15|3 1.25|4 2.9|5 2.7|6 2.75|
+------+------+------+------+------+
3 ave\ data
1 1.11667
2 1.2
3 1.76667
4 2.28333
5 2.78333
I think the dyadic /. (key) may be more interesting to
you than the monadic (oblique) for example:
key =. ? 20$5
prices =. key + ?20$0
0j2 ": key,: prices
2.00 2.00 4.00 3.00 0.00 0.00 1.00 1.00 3.00 0.00 3.00 2.00 3.00 2.00
3.00 3.00 1.00 2.00 3.00 4.00
2.35 2.67 4.56 3.31 0.30 0.61 1.40 1.58 3.94 0.30 3.76 2.95 3.31 2.47
3.80 3.73 1.47 2.46 3.40 4.76
key ave /. prices
2.57801 4.65735 3.60661 0.402511 1.48184
- joey
At 15:55 +1100 2008/01/04, Jack Andrews wrote:
> You mean like J's /. adverb?
looks interesting... i can kind of see how this could do moving
averages but can't see how you could apply it to a table like:
----+-----
day | price
----+------
0 | 1.00
1 | 1.20
2 | 1.15
3 | 1.25
4 | 2.90
5 | 2.70
6 | 2.75
...but in the following table, the sums and tallies of diagonals
contribute to a 3 day moving average:
----+-----
day | price
----+------
0-6 | 1.00 1.20 1.15 1.25 2.90 2.70 2.75
0-6 | 1.00 1.20 1.15 1.25 2.90 2.70 2.75
0-6 | 1.00 1.20 1.15 1.25 2.90 2.70 2.75
am i on the right track?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm