[R] generating symmetric matrices
gunter.berton at gene.com
Mon Jul 30 19:27:38 CEST 2007
See ?dist for an object oriented approach that may be better.
Directly, you can do something like (see ?row ?col):
x <- matrix(NA, 10,10)
## Lower triangular :
x[row(x) >= col(x) ] <- rnorm(55)
x[row(x) < col(x)] <- x[row(x) > col(x)]
## or you could have saved the random vector and re-used it.
Genentech Nonclinical Statistics
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gregory Gentlemen
Sent: Friday, July 27, 2007 8:28 PM
To: r-help at stat.math.ethz.ch
Subject: [R] generating symmetric matrices
I have a seemingly simple task which I have not been able to solve today. I
want to construct a symmetric matrix of arbtriray size w/o using loops. The
following I thought would do it:
p <- 6
Rmat <- diag(p)
dat.cor <- rnorm(p*(p-1)/2)
Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor
However, the problem is that the matrix is filled by column and so the
resulting matrix is not symmetric.
I'd be grateful for any adive and/or solutions.
[[alternative HTML version deleted]]
R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help