[R] Flattening and unflattening symmetric matrices

Nicholas Andrews noa at jhu.edu
Fri Apr 30 23:26:18 CEST 2010


Here's an easy question: I'd like to convert a symmetric matrix to a
vector composed of the upper.tri() part of it plus the diagonal, and
convert it back again.  What's the best way to achieve this?  I'm
wondering if there are some built in functions to do this easily.  I
can encode fine:

v <- c(diag(A),A[upper.tri(A)])

but I don't see an easy way to recover A from v without loops.  Any tips?



More information about the R-help mailing list