($ /: @: ; @: (|.&.>`]/.) @: (</.) @: i.)@:(2&#) 4
0 1 5 6
2 4 7 12
3 8 11 13
9 10 14 15
($ /: @: ; @: (|.&.>`]/.) @: (</.) @: i.)@:(2&#) 5
0 1 5 6 14
2 4 7 13 15
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24
The long part in the middle does the work:
make nxn index array
box the diagonals
reverse even-numbered diagonals
raze the result into a list
Then the /: which is the key to everything. The list computed
so far gives, for the diagonals in order, the original index
numbers of the items of the diagonal. For the 4x4 case, this is
z =. 0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
where I have put spaces between the diagonals. i{z is the original
index of element i in zigzag order. What we need is the inverse
of this permutation: we want o where i{o is z i. i . This is
just /: z .
To understand this step it helps to think of the array as a
16-element list rather than a 4x4 array.
Henry Rich
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of bill lam
> Sent: Saturday, November 25, 2006 9:44 AM
> To: Programming forum
> Subject: [Jprogramming] zig-zag order
>
> I found a problem that is said to an assignment for
> introductory C course but it
> turned to be not so trivial. It can be rephrased in J as: for
> a positive integer
> m find the array v or equivalently (m,m)$v so that result is
> a matrix with atoms
> of i.*:m filling in zig-zag order. (restricted to square
> matrix for time being)
>
> example
> 4 4$0 1 5 6 2 4 7 12 3 8 11 13 9 10 14 15
> 0 1 5 6
> 2 4 7 12
> 3 8 11 13
> 9 10 14 15
>
> I tackled the problem by noticing the sum of cell index pair
> for each layer is
> constant but run into trouble when need to eliminate extra
> pairs after passing
> the middle layer.
>
> All suggestions welcome.
>
> PS. zig-zag order is used in jpeg encoding
> http://en.wikipedia.org/wiki/JPEG
> --
> regards,
> bill
> ----------------------------------------------------------------------
> For information about J forums see
> http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm