Announcement

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

  • Mata loop with element multiplication

    Hi Statalist users!

    I an having a problem programing a rolling loop in mata. My goal is to take the rows of a matrix (matrix M in this case) and multiple them each together as a vector that I would code as the name of the rows that I multiplied together (example U11...U12...U13). However, when I run the code it writes over the row that I have created each time... and does not use my labeling designation from the code.

    This problem seems simple, but I'm stumped and would appreciate any help!

    Thanks,
    Nick

    Below is the code I am using.

    Code:
    for (i=1; i<=rows(M); i++) {
    for (j=1; j<=rows(M); j++){
    U`i'`j' = M[|i,.|]:*M[|j,.|]
    U`j'`i' = M[|j,.|]:*M[|i,.|]

    }
    }



  • #2
    Nick Harder --

    I don't think that Mata is recognizing the locals `i' and `j' in your routine. What I might recommend is storing your vectors in an associative array. Something like:
    Code:
    mata:
    M = runiform(10, 10)
    
    U = asarray_create("real",2)
    
    for (i=1; i<=rows(M); i++) {
        for (j=1; j<=rows(M); j++) {
            asarray(U,(i,j), M[i,]:*M[j,])
            asarray(U,(j,i), M[j,]:*M[i,])
        }
    }
    end
    Then, you can reference each of the vectors as follows:

    Code:
    mata:
    asarray(U, (1, 2))
    end

    Hope that helps!

    Matthew J. Baker

    Comment


    • #3
      Nick: I agree with Matthew that associative arrays are the way to go here.

      That said, I'll just note that there is an inelegant approach I've used in situations like yours. In essence you create a 1x1 string that becomes the argument to Mata's stata(...) interface function. E.g. you would replace line three of your code with something like
      Code:
      stata("mata: U"+strofreal(i)+strofreal(j)+" = M...")
      While I'm confident that there must be more-elegant ways to do this, associative arrays appear to me to be the better approach in any event.

      Comment


      • #4
        Matthew and John,

        Thank you! This has helped me tremendously with the problem at hand and in understanding mata!

        -Nick

        Comment


        • #5
          Hi
          I've probably misunderstood something, but why not use matrix multiplication?
          Like:
          Code:
          . mata M = rowshape(1..12, 3)
          
          . mata M
                  1    2    3    4
              +---------------------+
            1 |   1    2    3    4  |
            2 |   5    6    7    8  |
            3 |   9   10   11   12  |
              +---------------------+
          
          . mata M * M'
          [symmetric]
                   1     2     3
              +-------------------+
            1 |   30              |
            2 |   70   174        |
            3 |  110   278   446  |
              +-------------------+
          Kind regards/nhb
          Kind regards

          nhb

          Comment


          • #6
            Hi Niels,

            Thanks for the reply. For what I am doing I must use element wise multiplication. Otherwise that would work perfectly.
            -Nick

            Comment

            Working...
            X