Statalist The Stata Listserver


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

st: Triangular matrices


From   Brendan Halpin <[email protected]>
To   [email protected]
Subject   st: Triangular matrices
Date   Thu, 16 Nov 2006 23:06:14 +0000

I have data representing half of a symmetric matrix, in this form:

i  j   X
1  2  12
1  3  13
1  4  14
2  3  23
2  4  24
3  4  34

Is there an easy way of putting this into a matrix?

Currently I use an approach like this (variables are v1, v2 and v3,
matrix is A), but it feels clumsy:
-------------------------------------------------------------------
program define readdis          /*reads cols into squared matrix*/
   args dimension
   local trsize = `dimension'*(`dimension'-1)/2
   forvalues i = 1/`trsize' {
     local i1 = v1[`i']
     local j1 = v2[`i']
     matrix A[`i1',`j1'] = v3[`i']
     matrix A[`j1',`i1'] = v3[`i']
     }
end
-------------------------------------------------------------------

Is there a cleaner way?

Brendan
-- 
Brendan Halpin,  Department of Sociology,  University of Limerick,  Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F2-025 x 3147
mailto:[email protected]  http://www.ul.ie/sociology/brendan.halpin.html
*
*   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