| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Mata and nested loops
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
*
* 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/