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?
Dear Megh, Try this: a = matrix(rnorm(36), 6) a[upper.tri(a)]<-a[lower.tri(a)] a HTH, Jorge On Sun, Sep 21, 2008 at 1:47 PM, Megh Dal <megh700004@yahoo.com> 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? > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
try the following a <- matrix(rnorm(36), 6) ind <- lower.tri(a) a[ind] <- t(a)[ind] a I hope it helps. Best, Dimitris 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? > > ______________________________________________ > R-help at r-project.org 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014