1 2 Previous Next 16 Replies Latest reply: May 8, 2012 8:12 PM by MVittiS
MVittiS Level 1 Level 1 (0 points)

Hello.

 

In Numbers (iWork '08), is there any way to calculate the elements resulting from multiplying a Matrix (square table) to a Vector (single-column table) in another Vector (single-column table)?

 

I'm currently multiplying term by term, but it's a pain for lots of data. There must be a smarter way to do that.


Numbers '08, Mac OS X (10.7.3)
  • 1. Re: Advanced Math (Matrix * Vector)
    Jerrold Green1 Level 7 Level 7 (28,995 points)

    Numbers doesn't support Matrix math, not in either Numbers 08 or Numbers 09.

     

    Jerry

  • 2. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    So, is there any better way to accomplish the same operation using functions rather than multiplying term-by-term, cell-by-cell?

  • 3. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    there is a function called sumproduct() you can use to assist in this task:

    Screen Shot 2012-05-08 at 4.23.43 PM.png

  • 4. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    I've already tried, but I can find no practical use of SUMPRODUCT() in my specific problem; only if I copied the contents of the vector between every line of the matrix, and then used SUMPRODUCT() onto the pairs. It's better than nothing, but still hackishy.

  • 5. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    So if you have the following two matrices:

     

    V[10] and W[10, 10] then you want to perform operations to make a new matrix X which is 10 x 10 and is:

     

    V[0]*W[0, 0], V[1]*W[0, 1], ... V[9]*W[0, 9]

    V[0]*W[1, 0], V[1]*W[1, 1], ... V[9]*W[1, 9]

    .

    .

    .

    V[0]*W[9, 0], V[1]*W[9, 1],...  V[9]*W[9, 9]

     

     

    right?

  • 6. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    If the answer to my previous question is "Yes", then you can use a method like this:

    Screen Shot 2012-05-08 at 4.53.11 PM.png

     

    I am multiplying a 1 dimensional array times a two dimensional array resulting in a two dimensional array.

     

    To make this easier I transpose the 2D array, then multiply each row in table "Vector 1" by the corresponding row in each column of  the table "Vector 2".  This is stored in the table "Result Transposed".  Then I un-transpose and store in the table "Result".

     

    To do this we can start with transposing the table "Vector 2" into the table "Vector 2 Transposed" (the table with the highlighted cell in the image above):

     

    A1=OFFSET(Vector 2 :: $A$1, COLUMN()-1, ROW()-1, 1, 1)

    select A1 and fill to the right

    select A1 thru J1 and fill down

     

    Now let's put together the table "Result Transposed":

    A1=Vector 1 :: $A1*Vector 2 Transposed :: A1

    select A1 and fill DOWN

    now select A1 through A10 and fill to the right

     

    Now let's un trasnpose the data in table "Result Transposed" and store in the table "Result":

    in table "Result":

    A1=OFFSET(Result Transposed :: $A$1, COLUMN()-1, ROW()-1, 1, 1)

    select A1 and fill to the right

    now select A1 thru J1 and fill down

  • 7. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    That sure is a better way, but we're not done yet.

     

    You got close; my intention was to multiply what you called "Vector 1" with "Vector 2" (a Matrix, although mathematically speaking you would need to invert the terms order) resulting in another "Vector 3" (which would be a single-column table; each line the sum of all numbers in a line of "Result").

     

    Your method involves creating buffer/auxiliary tables... would there be a way to merge all these operations within a single formula, on the destination cell?

  • 8. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    With regards to:

    You got close; my intention was to multiply what you called "Vector 1" with "Vector 2" (a Matrix, although mathematically speaking you would need to invert the terms order) resulting in another "Vector 3" (which would be a single-column table; each line the sum of all numbers in a line of "Result").

     

    Please be specific with what you would like similar to the v[0]*W[0, 1]...

     

    The aux tables can be combined but I do not like doing that because:

    1) it makes reading what you did more difficult

    2) it make debugging more difficult

     

    As I indicated you can hide the aux tables so you won't see them.

  • 9. Re: Advanced Math (Matrix * Vector)
    Jerrold Green1 Level 7 Level 7 (28,995 points)

    MV,

     

    The free LibreOffice will probably do what you need with no sleight of hand and you can copy the results into a Numbers document if Numbers has other features you wish to retain.

     

    Jerry

  • 10. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    Ok. Let A[10] be a Vector, and B[10,10] a Matrix. B*A=C, wich is a Vector of type C[10]. So (in [Column, Line] notation) :

     

    C[0]=A[0]*B[0,0]+A[1]*B[1,0]+A[2]*B[2,0] (...) A[9]*B[9,0]

    C[1]=A[0]*B[0,1]+A[1]*B[1,1]+A[2]*B[2,1] (...) A[9]*B[9,1]

    (...)

    C[9]=A[0]*B[0,9]+A[1]*B[1,9]+A[2]*B[2,9] (...) A[9]*B[9,9]

     

    That's exactly the operation I'm trying to accomplish; a classical, mathematically defined, Vector * Matrix operation, wich results into a Vector of size equal to the Matrix's height and can only be made if the Vector's size and the Matrix's width are equal.

  • 11. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    Gotta try LibreOffice Spreadsheets later. I didn't knew I could copy data without having to save a .csv file for exchanging values between them. That sure is handy.

     

    As for now, I've (mostly) solved my problem by making a separate "scratch" table on which I can copy all the lines of the matrix, interleaved with the Vector's values, and obtain the results on a separate column nearby. The problem is that I cannot copy the data without Numbers trying to copy the formula too; I just want the values!

    Currently I'm retyping the results onto the definite result table.

     

    Everyone, thanks for the help.

  • 12. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    you can paste values only by using the menu item "Edit > Paste Values"

  • 13. Re: Advanced Math (Matrix * Vector)
    Wayne Contello Level 6 Level 6 (13,615 points)

    So based on your input I think you want something like this:

    Screen Shot 2012-05-08 at 9.39.49 PM.png

    the only difference is that the last table "Result" is now a single column with the formulas:

    A1=SUM(Result Transposed :: A1:J1)

    select A1 and fill down

     

     

    you can make the final solution look like this by:

    1) disabling the table name

    2) sliding the aux tables under the table "Vector 2"

     

    Screen Shot 2012-05-08 at 9.42.12 PM.png

  • 14. Re: Advanced Math (Matrix * Vector)
    MVittiS Level 1 Level 1 (0 points)

    Exactly what I described some messages ago. The only downside is having to use the transposed table (or transposed Vector (1), if that would eliminate the need for the Matrix transposing). If no matrix operations are supplied with Numbers, then I guess your method is the one who comes the closest.

1 2 Previous Next