# Re: st: A Question on Looping

 From Asgar Khademvatani <[email protected]> To [email protected] Subject Re: st: A Question on Looping Date Tue, 18 Sep 2007 15:22:28 -0600

Hello Nick,

First of many thanks for your invaluable comments and clues on the loop codes. On using " matrix symeigen" command you are correct. I had a typo mistake and i meant what you meant. I need only check with you on the basic definition of a symmetric matrix. A symmetric matrix ,by definition, is a quadratic matrix that its off-diagonal element are the same but its diagonal elements can be equal or not. Is not it?

Thank you very much again for the help.

Regards,
University of Calgary
n j cox wrote:

I see problems on various levels.

1. You are confusing the syntax of -foreach- and -forval-. Look
again at the syntax diagrams for these commands.

2. You are using subscript notation for two purposes, (a) to
refer to individual observations and (b) to tag different matrices.
The first is fine; the second is not correct syntax at all.
Stata does not have any concept of an array of matrices.

3. -matrix list- will only list one matrix at a time.

4. -matrix symeigen- will only play if your matrices are
symmetric.

This code should be more nearly legal, but it is not tested:

forval i = 1/43 {
matrix K = (A[`i'], B[`i'] \ B[`i'], A[`i'])
matrix symeigen X L = K
di "`i'"
matrix list L
}

However, I get the impression that you think -matrix symeigen-
will oblige you if your matrices are asymmetric. It will not.
In Stata 8.2, however, you could write your own code to
extract your eigenvalues as the roots of each characteristic
equation, which in this case is always a quadratic. In general,
some eigenvalues may be complex.

Alternatively, Stata 9 will happily oblige, or there are
other programs that will do this.

Nick
[email protected]

I have generated four variables A, B, C, and D that
each contain 43 observations, in Stata 8.2. I want to make 43 square
matrices (2 by 2) using each of the observations. Then I want to calculate the real eigenvalues of each matrix and list the 43 matrices
and their eigenvalues. I need to write a loop to do this job.

I have already written the following loop using -foreach- command but I am getting the following error as follows;

foreach i = 1/43 {
matrix K[`i'] = (A[`i'], B[`i'] \ C[`i'], D[`i'])
matrix symeigen X[`i'] V[`i'] = K[`i']
matrix list K[`i'] V[`i']
}
invalid syntax
r(198);

end of do-file
r(198);

Also, alternatively, I have written another alternative do file
with a -while- loop but it is also subject to error as follows;

1.local i 1
2. while `i' < 43 {
3. matrix K[`i'] = (lpl_m[`i'], lpm_m[`i'] \ le_m[`i'],lk_m[`i'])
4. matrix symeigen X[`i'] V[`i'] = K[`i']
5. matrix list V[`i']
6. local i = `i' + 1
7. }
too many ')' or ']'
r(132);

end of do-file
r(132);

Could you help me out on this matter and say what is wrong with any of
these alternative loops?

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