Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • matrix multiplication does not provide the expected result

    Hi All,

    I am confused by how Stata is multiplying values across two matrices.

    In the example below, the resulting values for all r X c = 8518, which is obviously not correct. The value for 69 * 69 (the r1/c1 * r1/c1) should be 4761... How do I get the correct results?

    Thanks!

    Ariel

    Code:
    mat d = (69, 34, -51 \ 69, 34, -51 \ 69, 34, -51)
    mat dt = d'
    Code:
    . mat li d
    
    d[3,3]
         c1   c2   c3
    r1   69   34  -51
    r2   69   34  -51
    r3   69   34  -51
    Code:
    . mat li dt
    
    dt[3,3]
         r1   r2   r3
    c1   69   69   69
    c2   34   34   34
    c3  -51  -51  -51
    Code:
    . mat T = d * dt
    
    . mat li T
    
    symmetric T[3,3]
          r1    r2    r3
    r1  8518
    r2  8518  8518
    r3  8518  8518  8518
    Code:
    . di (el(d,1,1) * el(dt,1,1))
    4761

  • #2
    Stata is correct in the matrix multiplication result. Instead, were you interested in element-wise multiplication? If so, element-wise operations are easier in Mata.

    Code:
    mata
    d = (69, 34, -51 \ 69, 34, -51 \ 69, 34, -51)
    d :* d'  /* element-wise multiplication */
    d * d'   /* matrix multiplication */
    end
    Result

    Code:
    : d :* d'
    [symmetric]
               1       2       3
        +-------------------------+
      1 |   4761                  |
      2 |   2346    1156          |
      3 |  -3519   -1734    2601  |
        +-------------------------+
    
    : d * d'
    [symmetric]
              1      2      3
        +----------------------+
      1 |  8518                |
      2 |  8518   8518         |
      3 |  8518   8518   8518  |
        +----------------------+
    To get element-wise multiplication in Stata's matrix language you can do this:

    Code:
    mat d = (69, 34, -51 \ 69, 34, -51 \ 69, 34, -51)
    mat dt = d'
    mat r = J(3,3, .)
    forval i = 1/3 {
      forval j = 1/3 {
        mat r[`i',`j'] = el(d,`i',`j') * el(dt, `i', `j')
      }
    }
    matlist r
    Result

    Code:
    . matlist r
    
                 |        c1         c2         c3
    -------------+--------------------------------
              r1 |      4761                       
              r2 |      2346       1156            
              r3 |     -3519      -1734       2601

    Comment


    • #3
      Thank you, Leonardo!

      It's interesting that addition and subtraction are element-wise, but multiplication is not...

      Thanks again!

      Ariel

      Comment


      • #4
        Actually, is there a way to write the Mata code in a single line, something along the lines of:

        Code:
        mata : st_matrix("d", * (st_matrix("dt")))

        Comment


        • #5
          I just figured it out!

          Code:
          mata : st_matrix("E", (st_matrix("d") :* (st_matrix("dt"))))

          Comment


          • #6
            Originally posted by Ariel Linden View Post
            Actually, is there a way to write the Mata code in a single line, something along the lines of:

            Code:
            mata : st_matrix("d", * (st_matrix("dt")))
            Yes you can do

            Code:
            mat d = (69, 34, -51 \ 69, 34, -51 \ 69, 34, -51)
            mata: st_matrix("res", (d=st_matrix("d")) :* d')
            mat list res
            Though my personal taste is to make this a little longer for readability.

            Comment


            • #7
              Thanks again, Leonardo!

              Comment


              • #8
                Yes, perhaps this
                Code:
                mata:st_matrix(“result”, st_matrix(“d”):*st_matrix(“d”)’)

                Comment


                • #9
                  Originally posted by Ariel Linden View Post
                  It's interesting that addition and subtraction are element-wise, but multiplication is not...
                  Matrix multiplication, addition and subtraction are defined operations. So Stata's choices are not arbitrary.

                  Comment

                  Working...
                  X