### Friday, February 24, 2006

## RE: st: Mata and nested loops

Dave

The answer is probably not where you are looking for it:

Your first example reads

"for (j=1, j<=3, j++) {"

where it should be

"for (j=1; j<=3; j++) {"

as in your second example.

Philippe

-------------------------------

st: Mata and nested loops From David Harless <dwharles@vcu.edu> To statalist@hsphsun2.harvard.edu Subject st: Mata and nested loops Date Fri, 24 Feb 2006 09:09:08 -0500 In answering a question on assigning rows of a matrix using -for- (http://www.stata.com/statalist/archive/2005-11/msg01018.html), Ben Jann gave an example

mata A = (1,2,3\4,5,6) C = J(2,3,.) for (i=1; i<=2; i++) { C[i,.] = A[i,.] C[i,.] // display } end

I tried to make a simple modification to this so that each element in the matrix was assigned in nested -for- loops rather than assigning a row at a time.

mata A = (1,2,3\4,5,6) C = J(2,3,.) for (i=1; i<=2; i++) { for (j=1, j<=3, j++) { C[i,j] = A[i,j] } } C end

But this generates the error: unexpected end of line -------------------------------------------- r(3000);

end of do-file r(3000);

The nested loop works fine in other simple examples such as

mata C = J(2,3,.) for (i = 1; i<=rows(C);i++) { for (j = 1; j<=cols(C);j++) { C[i,j] = i+j } } C end

But not when I make the element by element assignment C[i,j] = A[i,j] above.

Thanks for your consideration, Dave Harless

********************************************************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager.

This footnote also confirms that this email message has been swept **********************************************************************

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

Tag: statalist