Let’s say we have a rank 3 array A like so:
]A=.i.2 4 7
0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34
35 36 37 38 39 40 41
42 43 44 45 46 47 48
49 50 51 52 53 54 55
And we would like to add the rows. The rank conjunction along the second
dimension seems to work fine in this case.
+/"2 A
42 46 50 54 58 62 66
154 158 162 166 170 174 178
Now let’s say we’d like to add the columns. The rank conjunction along the
first dimension is employed in this case to produce the result.
+/"1 A
21 70 119 168
217 266 315 364
It is this result where I, being a J novice, perceive that an unexpected
transposition has taken place. I’ll explain what formed my expectations.
Coming from a Matlab background I’m used to expect the results in a certain
spatial location. For example, if I wanted to replicate the same results in
Matlab I would do something like the following with A being a 4 by 7 by 2
array (as opposed to a 2 by 4 by 7 in J, but this is a very minor
difference and one can adjust to it easily).
>> A
A(:,:,1) =
0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
A(:,:,2) =
28 29 30 31 32 33 34
35 36 37 38 39 40 41
42 43 44 45 46 47 48
49 50 51 52 53 54 55
>> sum(A,1)
ans(:,:,1) =
42 46 50 54 58 62 66
ans(:,:,2) =
154 158 162 166 170 174 178
>> sum(A,2)
ans(:,:,1) =
21
70
119
168
ans(:,:,2) =
217
266
315
364
So Matlab’s sum(A,1) corresponds to J’s +/”2 A, but Matlab’s sum(A,2)
corresponds to a visually transposed form of J’s +/”1 A. In other words,
the shape of the result of adding columns in Matlab carries a spatial
resemblance to performing the addition of the columns of A on a chalkboard
and writing the result in a vertical manner for each successive row.
I suspect there are good reasons for this behavior in J that allow for easy
scaling of the results in higher dimensions. I think my fear is that this
behavior might lead me to erroneous indexing due to not having the proper
visualization of the results. For example in Matlab I could find result of
the sum of the columns of A for the third row of the first 2D slice at the
position (3,1,1) of the result of sum(A,2). Whereas I’m not sure about the
proper way of indexing on the results of +/”1 A.
I have observed this behavior elsewhere in J as well. For example using the
dyad ,. (stitch) on i.5 and 1+i.5, seems to first transpose the row vectors
and then appending to the right, whereas I was expecting the row vector 0 1
2 3 4 1 2 3 4 5 as output.
i.5
0 1 2 3 4
1+i.5
1 2 3 4 5
(i.5),.(1+i.5)
0 1
1 2
2 3
3 4
4 5
My question to the J community then is with regards to the appropriate way
to think about arrays in J so that I can correctly anticipate vector
transposition when applying the rank conjunction and how to properly index
on its result.
Thank you,
George
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm