Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: foreach and mata end


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: foreach and mata end
Date   Tue, 7 Feb 2006 16:36:32 -0000

Try replacing

mata
mdiag(`mname')
end

With

mata: mdiag(`mname')

David

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Matissa
Hollister
Sent: 07 February 2006 16:21
To: [email protected]
Subject: st: foreach and mata end

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/

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index