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

st: RE: a question about matrix looping


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: a question about matrix looping
Date   Wed, 29 Oct 2003 11:19:57 -0000

CUI liwei
 
> Here is my data of matrix.
> 
> . matrix list a1
> 
> a1[1,5]
>            p1         p2         p3         p4         p5
> c1  2575.8327  500.06787  186.97998   254.6509  1106.3182
> 
> 
> . matrix list p
> 
> p[5,5]
>            p1         p2         p3         p4         p5
> r1  .66871315  .08958182  .02526662  .03012322  .18631518
> r2  .33131433  .22863834  .08614853  .07708991  .27680892
> r3  .13114183  .09663552  .09053405  .20670988  .47497872
> r4  .13119115  .07068948  .07335503  .16629378  .55847055
> r5          0          0          0          0          0
> 
> 
> I want to get a2, a3 ....a10.
> 
> . matrix a2=a1*p
> . matrix a3=a2*p
> .
> .
> .
> . matrix a10=a9*p
> 
> to make it easy, I wrote the code, but it doesn't work.
> 
> while `i'<=10{
>     matrix a`i+1'=a`i'*p
>     local i=`i'+1
>     }
> 
> and at last I want convert the matrix a1 a2 ...a10 to variables.

`i+1' is not going to increment the macro on the fly in any version 
of Stata. 

In Stata 7 or 8, you can do this 

local i = 1 
while `i' <= 10 {
	matrix a`=`i'+1' = a`i' * p
      local i=`i'+1
}

or 

forval i = 1/10 { 
	matrix a`=`i'+1' = a`i' * p
}

See also Deborah Garvey's question on 28 August
and my reply on 29 August. 
 
Nick 
[email protected] 

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