[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: vec of upper triangle
Edwin Leuven <[email protected]> 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
[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/