Statalist The Stata Listserver


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

Re: st: Mata and nested loops...Thanks


From   David Harless <[email protected]>
To   [email protected]
Subject   Re: st: Mata and nested loops...Thanks
Date   Fri, 24 Feb 2006 09:54:35 -0500

Phillippe,
How is it that one can look at something
repeatedly and miss something like that?

Thanks so much for taking the trouble to
point out my error.

Dave Harless

philippe van kerm wrote:

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 <[email protected]>
To [email protected]
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/

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