First of all, rank conjunction <> axis specification. Matlab
seems using axis specification although I know nothing about it.

+/ or +/"r always insert + between "items" but will not change the
order of axis. Items of a vector is scalar; Items of a matrix is
its row; item of i.2 4 7 is a matrix of shape 4 by 7, etc. 

the sentence 

 +/"1 i. 4 7

looks as if it adds across columns but actually it does not.
J Implementation may add a whole column ot another column, but
conceptually, +/"1 applies to each of the 4 rows to give 4 scalar sums.
and then they are assembled to form a 4-element vector. It does
not involves any transpose.

I hope this will not make you even more confusing.

Returning to your original problem, J cannot compute the sum by
column using rank conjunction only, instead it need a transpose
to change the order of axis.

try different combinations to choose what you wanted, eg

 +/"1 |:"1 i.2 4 7
 +/"1 |:"2 i.2 4 7
 +/"2 |:"1 i.2 4 7
 +/"2 |:"2 i.2 4 7
 +/ |:"1 i.2 4 7
 +/ |:"2 i.2 4 7


Чт, 11 сен 2014, George Dallas написал(а):
> 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

-- 
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
gpg --keyserver subkeys.pgp.net --armor --export 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to