Ranjan Maitra
2007-Apr-20 01:27 UTC
[R] how to convert the lower triangle of a matrix to a symmetric matrix
Hi, I have a vector of p*(p+1)/2 elements, essentially the lower triangle of a symmetric matrix. I was wondering if there is an easy way to make it fill a symmetric matrix. I have to do it several times, hence some efficient approach would be very useful. Many thanks and best wishes, Ranjan
Charles C. Berry
2007-Apr-20 03:53 UTC
[R] how to convert the lower triangle of a matrix to a symmetric matrix
On Thu, 19 Apr 2007, Ranjan Maitra wrote:> Hi, > > I have a vector of p*(p+1)/2 elements, essentially the lower triangle of > a symmetric matrix. I was wondering if there is an easy way to make it > fill a symmetric matrix. I have to do it several times, hence some > efficient approach would be very useful.It depends on what the ordering of elements in your vector is. Many programs will order those elements (1,1), (2,1), (2,2), (3,1) , ... In which case this shows how:> k.given.i.j <- function(x , y ) ifelse( y<x, x*(x-1)/2 + y, y*(y-1)/2 + x ) > k.mat <- function(p) outer( 1:p, 1:p, k.given.i.j ) > k.mat( 3 )[,1] [,2] [,3] [1,] 1 2 4 [2,] 2 3 5 [3,] 4 5 6> matrix( rnorm( choose(4,2) )[ k.mat( 3 ) ] , nr = 3 )[,1] [,2] [,3] [1,] -1.2165313 0.28262740 0.62448849 [2,] 0.2826274 -1.19842868 0.05676263 [3,] 0.6244885 0.05676263 -1.80957190>For efficiency, you might save and reuse the result of k.mat(). If the elements are ordered (1,1), (2,1), (3,1), ..., you will need to write your own version of k.given.i.j()> > Many thanks and best wishes, > Ranjan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0901
John Fox
2007-Apr-20 12:50 UTC
[R] how to convert the lower triangle of a matrix to a symmetricmatrix
Dear Ranjan, If the elements are ordered by rows, then the following should do the trick: X <- diag(p) X[upper.tri(X, diag=TRUE)] <- elements X <- X + t(X) - diag(diag(X)) If they are ordered by columns, substitute lower.tri() for upper.tri(). I hope this helps, John -------------------------------- John Fox, Professor Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ranjan Maitra > Sent: Thursday, April 19, 2007 9:28 PM > To: r-help at stat.math.ethz.ch > Subject: [R] how to convert the lower triangle of a matrix to > a symmetricmatrix > > Hi, > > I have a vector of p*(p+1)/2 elements, essentially the lower > triangle of a symmetric matrix. I was wondering if there is > an easy way to make it fill a symmetric matrix. I have to do > it several times, hence some efficient approach would be very useful. > > Many thanks and best wishes, > Ranjan > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Liaw, Andy
2007-Apr-20 13:44 UTC
[R] how to convert the lower triangle of a matrix to a symmetricmatrix [Broadcast]
Ranjan and Prof. Fox, Similar approach can be found in stats:::as.matrix.dist(). Andy From: John Fox> > Dear Ranjan, > > If the elements are ordered by rows, then the following > should do the trick: > > X <- diag(p) > X[upper.tri(X, diag=TRUE)] <- elements > X <- X + t(X) - diag(diag(X)) > > If they are ordered by columns, substitute lower.tri() for > upper.tri(). > > I hope this helps, > John > > -------------------------------- > John Fox, Professor > Department of Sociology > McMaster University > Hamilton, Ontario > Canada L8S 4M4 > 905-525-9140x23604 > http://socserv.mcmaster.ca/jfox > -------------------------------- > > > -----Original Message----- > > From: r-help-bounces at stat.math.ethz.ch > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ranjan Maitra > > Sent: Thursday, April 19, 2007 9:28 PM > > To: r-help at stat.math.ethz.ch > > Subject: [R] how to convert the lower triangle of a matrix to > > a symmetricmatrix > > > > Hi, > > > > I have a vector of p*(p+1)/2 elements, essentially the lower > > triangle of a symmetric matrix. I was wondering if there is > > an easy way to make it fill a symmetric matrix. I have to do > > it several times, hence some efficient approach would be > very useful. > > > > Many thanks and best wishes, > > Ranjan > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}