Note that Henry is using the J 6.01 beta for this to work.

Also, since you are using a 3-wide window, you could use
a general average function (+/%#) instead of "average 3 things".

So, in the current non-beta version of  J (5.04), you could write
Henry's expression like this:
  upd=:  3 : '(3 (+/%#)\"1 y.)(<<<0 _1)}"1 y.'

I also re-arranged the terms by using an extra set of parens rather
than a tilde and define it using explicit notation: these are personal
preferences.

One tricky part Henry did not fully explain is that
  (<<<0 _1)}"1
is inserting into the columns (the }"1 part) but excluding the
0th and _1th (last) column; the triple boxing (<<<) achieves the exclusion.
Notice how the array expression highlights the special handling of
the first and last column whereas this is more implicit in the looping code.

Might as well also demonstrate that it works on a higher-dimensional
array as well:
  ]aa=. ?2 3 4$100
90 47 58 29
22 32 55  5
55 73 58 50

40  5 69 46
34 40 46 84
29  8 75 97
  upd aa
90        65 44.666667 29
22 36.333333 30.666667  5
55        62 60.333333 50

40        38        40 46
34        40 56.666667 84
29 37.333333        60 97

Devon

On 5/30/06, Henry Rich <[EMAIL PROTECTED]> wrote:

A J program might be:

upd =:  (<<<0 _1)}"1~    3 (3 %~ +/)\"1 ]


The computation of the moving average is done by
  3 (3 %~ +/)\"1 ]
which averages successive 3-element sections.

Then there is the question of what to do with the unchanged ends
of the array; this program decides to leave them as is, and
inserts the result of the computation into the array, using
  (<<<0 _1)}"1~

This program should work on arrays of any dimension.

...
--
Devon McCormick
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to