Announcement

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

  • Replacing a row in a matrix using matrix define

    I am writing a program which is designed to be run repeatedly for each day in (say) a month. It uses a Mata function to run a linear program for each day. The function returns a set of results in a row vector that I want to store in matrix whose dimensions r, c are the number of days in the month and the number of variables. The problem is that there seems to be no way of replacing the whole row of a matrix by writing:

    matrix R[day,1...] = matares

    where R is the matrix used to store the daily results and matares is the row vector returned by the Mata function. Whatever variant I try to refer to a row of R[] on the LHS of a matrix define statement generates a syntax error.

    There are at least two brute force ways of getting round the problem. The easiest is:

    forval n = 1/`nv' {
    matrix R[day,`n'] = result[1,`n']
    }

    This is very inefficient as matrix operations are always better than element-by-element operations.

    The alternative is to carry out the replacement in the Mata function by passing R to the function, but I don't like this because I am already passing a lot of variables to the function and the code is looking too spaghetti-like. In addition, I would prefer to avoid the necessity of the Mata function knowing what day of the month it is handling.

    What am I missing? Is there some way that I can use matrix to replace a row in a matrix rather than an element. By the way I have thought of using

    matrix R=R \ matares

    but that is vulnerable to out-of-order execution for days in the month and is hard to make work for the first day in the month.

  • #2
    I don't know how much this helps.

    Code:
    . matrix M = (1,2,3\4,5,6\7,8,9)
    
    . matrix m = (42, 666, 1952)
    
    
    . matrix N = M[1,1..3] \ m \ M[3,1..3]
    
    . mat li N
    
    N[3,3]
          c1    c2    c3
    r1     1     2     3
    r1    42   666  1952
    r3     7     8     9

    Comment


    • #3
      Thank you. That works. Still, it is clumsy to code because one has to deal separately with the cases in which the day is the first of the month or the last of the month. And with varying numbers of days in the month plus leap years. In terms of efficiency I suspect it depends on the number of variables (columns in the matrix). At the moment I am using the brute force method I describe but if offends my sense of elegance.

      Comment


      • #4
        I prize elegance too and for that reason and others don't use Stata's matrix language much any more when Mata is available.

        Comment


        • #5
          I agree with you fully in preferring Mata over Stata matrix as a general rule, but this is a particular context in which I am trying to store results from a Mata function. Underlying this is the fact that I find the way in which Mata handles communication & data transfer between Mata & Stata to be unintuitive and obscure. It is difficult to master unless one is writing Mata code all of the time. A period away and you have to go up a steep learning curve all over again. The rest of Mata is just matrix algebra. I co-wrote a textbook on the subject so I have something to fall back on.

          Comment

          Working...
          X