I've stored a number of results from an analysis as
mata matrices and now I'm trying to do a series of
operations on all the matrices. Since the matrix
names are strings, the only way I could think of
iterating across the matrices was to create a foreach
loop, but entering and exiting mata in the loop causes
problems. I've figured out a work around, but I'm
wondering if I'm missing something. Here's a simple
example that illustrates the problem:
------do file---------------------
mata
A=(1,2,3\4,5,6\7,8,9)
B=(9,8,7\6,5,4\3,2,1)
C=(3,3,3\4,4,4\5,5,5)
void mdiag(mname)
{
n=rows(mname)
for (j=1;j<=n;j++) {
mname[j,j]=.
}
}
end
foreach mname in A B C {
mata
mdiag(`mname')
end
}
--------------------------------
If I run the do file above, the output ends with the
following:
. foreach mname in A B C {
2. mata
3. mdiag(`mname')
4. end
--Break--
r(1);
end of do-file
--Break--
r(1);
It seems that within a foreach loop, typing end is
read as breaking the loop, which stops the whole do
file. The following works fine:
-----------new end to do file----------------
mata
mdiag(A)
end
mata
mdiag(B)
end
mata
mdiag(C)
end
-----------------------------------
So just typing "end" itself doesn't cause a problem,
only if it's within a loop. I can also get the do
file to run just fine if I put the loop commands in a
separate do file:
-------new end to main do file-----------
foreach mname in A B C {
do "loop.do" `mname'
}
--------loop.do------------------
mata
mdiag(`1')
end
---------------------------
So, is it just a bad idea to enter and exit mata
within a foreach loop? Is there a way to iterate
across a list of string values and stay within mata?
Thanks for your help as I continue to muddle my way
through mata.
Matissa
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
*
* 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/