(flashing I-am-bad-at-J disclaimer)
Consider:
]a=.i.20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
_3[\ a
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16 17
18 19 0
every third element's right there in the first column.
thus:
th =: 1 : ',(u@:{."1 ,. }."1) (-x)[\y'
which reshapes the list, applies the verb to the first column, then
stitches the result of that to the other columns, then returns a list.
3 (_"_) th a
_ 1 2 _ 4 5 _ 7 8 _ 10 11 _ 13 14 _ 16 17 _ 19 0
4 (_"_) th a
_ 1 2 3 _ 5 6 7 _ 9 10 11 _ 13 14 15 _ 17 18 19
5 (_"_) th a
_ 1 2 3 4 _ 6 7 8 9 _ 11 12 13 14 _ 16 17 18 19
The resulting list can be padded with zeroes, but since the passed verb
will never apply to those, you can just strip them back off:
th =: 1 : '(#y) $ ,(u@:{."1 ,. }."1) (-x)[\y'
19 (_"_) th a
_ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 _
NB. ^^^not still padded with 18 zeroes
On Wed, 2020-01-15 at 20:30 -0600, Skip Cave wrote:
> How does one apply a monadic verb repetitively to every nth element
> of a
> one1-dimentional array?
>
> For example:
>
> ]a=.i.20
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> n=.3
>
> NB. Design u to increment every nth integer in a:
>
>
> u=.??:<??
> n u a
> 1 1 2 4 4 5 7 7 8 10 10 11 13 13 14 16 16 17 19 19
> -------------------------------------------------------------------
> ---
> For information about J forums see
> http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm