[R] Need help on upper.tri()
Ravi Varadhan
rvaradhan at jhmi.edu
Tue Aug 3 18:44:33 CEST 2010
There are other ways to make symmetric matrices:
1. mat + t(mat)
2. crossprod(mat)
3. tcrossprod(mat)
(1) is slightly faster than (2) and (3) (difference is trivial except for very large matrices), but (2) and (3) are guranteed to give you a positive-semidefinite (PSD) matrices, whereas (1) is not.
Ravi.
____________________________________________________________________
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University
Ph. (410) 502-2619
email: rvaradhan at jhmi.edu
----- Original Message -----
From: William Dunlap <wdunlap at tibco.com>
Date: Tuesday, August 3, 2010 12:28 pm
Subject: Re: [R] Need help on upper.tri()
To: Nikhil Kaza <nikhil.list at gmail.com>, Ron Michael <ron_michael70 at yahoo.com>
Cc: r-help at r-project.org
> > -----Original Message-----
> > From: r-help-bounces at r-project.org
> > [ On Behalf Of Nikhil Kaza
> > Sent: Tuesday, August 03, 2010 8:56 AM
> > To: Ron Michael
> > Cc: r-help at r-project.org
> > Subject: Re: [R] Need help on upper.tri()
> >
> >
> > try using Matrix package instead
> >
> > mat <- Matrix(rnorm(25),5,5)
> > forceSymmetric(mat)
> >
> > The reason your method does not work is because matrix is
> > effectively
> > a vector and the indices increase along rows within a column.
>
> To copy the transpose of the upper triangle to the
> lower triangle using only base R functions try
> mat[lower.tri(mat)] <- t(mat)[lower.tri(mat)]
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> >
> > Nikhil
> >
> > On Aug 3, 2010, at 7:36 AM, Ron Michael wrote:
> >
> > > HI, I am really messing up to make a symmetrical matrix using
> > > upper.tri() & lower.tri() function. Here is my code:
> > >
> > >> set.seed(1)
> > >> mat = matrix(rnorm(25), 5, 5)
> > >> mat
> > > [,1] [,2] [,3] [,4] [,5]
> > > [1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737
> > > [2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630
> > > [3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498
> > > [4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170
> > > [5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575
> > >> mat[lower.tri(mat)] = mat[upper.tri(mat)]
> > >> mat
> > > [,1] [,2] [,3] [,4] [,5]
> > > [1,] -0.62645381 -0.82046838 1.51178117 -0.04493361 0.91897737
> > > [2,] -0.82046838 0.48742905 0.38984324 -0.01619026 0.78213630
> > > [3,] 1.51178117 -0.01619026 -0.62124058 0.94383621 0.07456498
> > > [4,] 0.38984324 0.94383621 0.78213630 0.82122120 -1.98935170
> > > [5,] -0.04493361 0.91897737 0.07456498 -1.98935170 0.61982575
> > >
> > >
> > > Which is not coming as symmetrical function. Can anyone
> > point me on
> > > the correct way of using upper, lower.try() function to get a
> > > symmetrical matrix?
> > >
> > > Thanks,
> > >
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > >
> > > PLEASE do read the posting guide
> >
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> >
> > PLEASE do read the posting guide
> >
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
>
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list