Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Re: A Mata question.


From   "Salvati, Jean" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Re: A Mata question.
Date   Mon, 28 Nov 2005 16:02:26 -0500

Benn Jann wrote:
> Matrix subscripts are typed in brackets.

I think what Amadou wants is an array or "collection" of matrices. And
Amadou is confused because macro substitution doesn't work.

I have very little experience with MATA, but it seems to me that the
desired result can be achieved using pointers and arrays of pointers.
Here is an example. For i=1 to i=10, the following program creates and
displays a 2x2 matrix filled with i:

mata

	// declare an array of NULL pointers
	P = J(10,1,NULL)

	for (i=1;i<10;i++) {

		// create a matrix and assign its address to P[i]
		P[i] = &(J(2,2,i))

		// just make sure that all the elements of P don't point
to the same matrix

	}

	for (i=1;i<10;i++) {

		i
		// dereference the i-th element of P to get the i-th
matrix in the "collection"
		*(P[i])

	}
end

The alternative is to create a single big matrix and extract rows from
that matrix as needed.

Jean Salvati

> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On Behalf Of 
> > [email protected]
> > Sent: Monday, November 28, 2005 7:52 PM
> > To: [email protected]
> > Subject: st: Re: A Mata question.
> > 
> > 
> > Thanks Nick for the answer.
> > My other question is how to create
> > new matrix within loops?
> > I am puzzled by the fact that there is no `i'
> > in mata's loops as in normal stata.
> > Best regards.
> > Amadou.
> > 
> > mata:
> > A1 = (1,2,3\4,5,6)
> > A1
> > A2 = (1,2,3\4,5,6)
> > A2
> > B1=A1
> > B1
> > B2=A2
> > B2
> > for (i=1;i<=2;i++) {
> >  i // just to display the increment
> >  C(i) = A(i) // ??? I tried Ci = Ai and C`i' = A`i' plus various 
> > variants (==, (), etc...) with no success
> >  C(i) // display
> > }
> > end:
> > 
> > 
> > *
> > *   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/
> 

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