[R] Symmetric matrix
Martin Maechler
maechler at stat.math.ethz.ch
Mon Sep 22 09:16:52 CEST 2008
>>>>> "DR" == Dimitris Rizopoulos <d.rizopoulos at erasmusmc.nl>
>>>>> on Sun, 21 Sep 2008 19:58:44 +0200 writes:
DR> try the following
DR> a <- matrix(rnorm(36), 6)
DR> ind <- lower.tri(a)
DR> a[ind] <- t(a)[ind]
DR> a
Yes, indeed, it needs the t(.) trick.
Note that 'Matrix' package has a function forceSymmetric(.) to
do this for you (faster, using C code):
A <- forceSymmetric(Matrix(rnorm(36), 6))
is all you'd need {if can afford to trash half of the random
numbers generated}
Martin Maechler, ETH Zurich
DR> I hope it helps.
DR> Best,
DR> Dimitris
DR> Megh Dal wrote:
>> I have following matrix :
>>
>> a = matrix(rnorm(36), 6)
>>
>> Now I want to replace the lower-triangular elements with it's upper-triangular elements. That is I want to make a symmetric matrix from a. I have tried with lower.tri() and upper.tri() function, but got desired result. Can anyone please tell me how to do that?
More information about the R-help
mailing list