Friday, February 24, 2006

st: Mata and nested loops

In answering a question on assigning rows of a matrix using -for- (, 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: * * *


Links to this post:

Create a Link

<< Home

This page is powered by Blogger. Isn't yours?