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.
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.
Comment