Partha Deb <[email protected]> writes,
> I'm trying to create a number of matrices in Mata ordered, e.g., x1, x2,
> ..., x7. I'd like to set up a loop to create these. If these were Stata
> objects, I could do them quite easily, e.g., I might write something like
>
> forvalues i=1/7 {
> gen x`i' = `i'
> }
>
> but I can't seem to figure out how to do something similar within Mata.
Exactly right. My first recommendation is to use subroutines and
simply call seven times:
mat_init(x1)
mat_init(x2)
...
mat_init(x7)
If Partha is absolutely determined to work in a loop, then she is going to
have to learn about pointers. The setup would be
p = J(7, 1, NULL)
p[1] = &x1
p[2] = &x2
...
p[7] = &x7
and then Partha could code things like
for (i=1; i<=7; i++) {
mat_init(*p[i])
}
or
for (i=1; i<=7; i++) {
*p[i] = J(4,4,0)
for (j=1; j<=4; j++) (*p[i])[j] = 1
}
In terms of initialization of p[], unless it was important to be able to
refer to them by the names x1, x2, ..., x7, one could simply the setup to
p = J(7, 1, NULL)
for (i=1; i<=7; i++) p[i] = &J(0,0,.)
There are some problems for which pointers are wonderful solutions. In a
problem like this one, however, my guess is that pointers are not worth
the effort.
-- Bill
[email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/