# how to convert the lower triangle of a matrix to a symmetric matrix

6 messages
Open this post in threaded view
|

## 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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 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 > > ______________________________________________ > [hidden email] 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:[hidden email]         UC San Diego http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0901 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to convert the lower triangle of a matrix to a symmetricmatrix

 In reply to this post by Ranjan Maitra 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: [hidden email] > [mailto:[hidden email]] On Behalf Of Ranjan Maitra > Sent: Thursday, April 19, 2007 9:28 PM > To: [hidden email] > 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 > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: 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: [hidden email] > > [mailto:[hidden email]] On Behalf Of Ranjan Maitra > > Sent: Thursday, April 19, 2007 9:28 PM > > To: [hidden email] > > 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 > > > > ______________________________________________ > > [hidden email] 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. > > > > ______________________________________________ > [hidden email] 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}} ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.