Search code examples
vectormatrixmultiplicationj

Multiply the rows of a matrix to get a vector: J, j701


I am programming with J. I have this vector:

    F =: 5>\i.10
    F
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

How can I have this vector as result:

   (*/ 0 1 2 3 4), (*/ 1 2 3 4 5), (*/ 2 3 4 5 6), (*/ 3 4 5 6 7), (*/ 4 5 6 7 8), (*/ 5 6 7 8 9)
0 120 720 2520 6720 15120
   NB. I want to multiply all the rows

I tried:

   */ F
0 720 5040 20160 60480

but, how you can see it multiply the columns, and I want the rows. How can I use the */ to multiply the rows? Thank you all!


Solution

  • In short, what you want is 5 */\ i.10

       5 */\ i.10
    0 120 720 2520 6720 15120
    

    However, if you ever run across this issue in another context, and you really want to address the rows, you could say:

       ]M=:5>\i. 10
    0 1 2 3 4
    1 2 3 4 5
    2 3 4 5 6
    3 4 5 6 7
    4 5 6 7 8
    5 6 7 8 9
    
       */ rows M
    0 120 720 2520 6720 15120
    

    Rows is defined by the standard library as "1. That is, it applies the verb at "rank 1". Rank is a fundamental concept in J, and you'll need to understand it to progress with the language.