Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: vec of upper triangle


From   "David M. Drukker" <ddrukker@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: vec of upper triangle
Date   Wed, 9 Aug 2006 07:37:17 -0500 (CDT)

Edwin Leuven <e.leuven@uva.nl> asked if there is

a good (fast) way of putting the upper triangle of a square matrix into a vector?

example:

1 2 3
2 4 5
3 5 6

into:

1
2
4
3
5
6

Yes, use the -vech()- function in Mata. Here is an example.

. mata: ------------------------------------------------- mata (type end to exit) ------------------------------------
: : x = 1::3

: x
1
+-----+
1 | 1 |
2 | 2 |
3 | 3 |
+-----+

: A = x*x'

: A
[symmetric]
1 2 3
+-------------+
1 | 1 |
2 | 2 4 |
3 | 3 6 9 |
+-------------+

: vech(A)
1
+-----+
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 6 |
6 | 9 |
+-----+


-vech()- does not require that the matrix be symmetric and it uses the
lower triangle in forming the stacked elements.

David
ddrukker@stata.com

*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index