j - Modifying one row of an array -


i've started learning j , there's have no idea how properly

suppose want print checkerboard of 2 symbols, example

baba abab baba 

to this, assumed generate array

baba baba baba 

and reverse second line.

generating array easy: 3 4 $ 'ba'. reversing second row struggle.

i can reverse of second row doing |. 1 { 3 4 $ 'ba' gives me second row, not entire array. don't see how using j syntax can keep top , bottom row , apply |. middle row.

more generally, how apply |. every other row?

what asked

to apply |. 1 row, try like:

   x =: 3 4 $ 'ba'    (|. 1{x) 1} x baba abab baba 

to reverse every other row, don't know if there's simpler this:

   ,/ 1 (]`(|."1))\ i. 5 4  0  1  2  3  7  6  5  4  8  9 10 11 15 14 13 12 16 17 18 19 

this uses relatively obscure feature of dyad \ (infix):

x m\ y applies successive verbs gerund m infixes of y, extending m cyclically required.

here, x 1, our "infixes" 1×4 matrices; cycle through gerund (] ` (|."1)) alternate between doing nothing (]) , reversing single row of submatrix (|."1). then, flatten resulting 5×1×4 array 5×4 matrix ,/.

what maybe want instead

a simpler way achieve "checkerboard" follows: first, use +/ on 2 ranges create "addition table", so:

   (i.3) +/ (i.4) 0 1 2 3 1 2 3 4 2 3 4 5 

then take of these values mod 2, checkerboard pattern of 0s , 1s:

   2 | (i.3) +/ (i.4) 0 1 0 1 1 0 1 0 0 1 0 1 

then index string of choice {:

   (2 | (i.3) +/ (i.4)) { 'ba' baba abab baba 

Comments