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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Ranjan Maitra
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.
Reply | Threaded
Open this post in threaded view
|

Re: how to convert the lower triangle of a matrix to a symmetric matrix

cberry
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
>
> ______________________________________________
> [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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Fox, John
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: how to convert the lower triangle of a matrix to a symmetricmatrix [Broadcast]

Liaw, Andy
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

olivier Eterradossi-3
In reply to this post by Fox, John
Sorry if this answer was already given, or if I miss the point,
but did you have a look to "lowerTriangle" and "upperTriangle" functions
in the "gdata" package ?

# example
# A<-matrix(rnorm(9),3,3)
# B<-B<-matrix(NA,dim(A)[1],dim(A)[2])
# lowerTriangle(B)<-lowerTriangle(A)
# upperTriangle(B)<-lowerTriangle(A)
# diag(B)<-diag(A)

Hope this helps, sorry if it was already answered.
Olivier

--
Olivier ETERRADOSSI
Maître-Assistant
CMGD / Equipe "Propriétés Psycho-Sensorielles des Matériaux"
Ecole des Mines d'Alès
Hélioparc, 2 av. P. Angot, F-64053 PAU CEDEX 9
tel std: +33 (0)5.59.30.54.25
tel direct: +33 (0)5.59.30.90.35
fax: +33 (0)5.59.30.63.68
http://www.ema.fr

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

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

marella
@Olivier ETERRADOSSI :

I tried "gdata" to convert a (10000x10000) lower triangular matrix to full matrix and got memory problems. Especially at this step :

upperTriangle(a) <- lowerTrangle(a)

it consumes more than 3 GB . Any ideas on how to create full matrix with efficient memory management ?