[R] generating symmetric matrices
Charles C. Berry
cberry at tajo.ucsd.edu
Mon Jul 30 18:39:29 CEST 2007
On Fri, 27 Jul 2007, Gregory Gentlemen wrote:
> Greetings,
>
> 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.
Depends on the order of elements in dat.cor. Either
Rmat <- diag(p)
Rmat[lower.tri(Rmat)] <- dat.cor
Rmat[upper.tri(Rmat)] <- t( Rmat )[upper.tri(Rmat)]
or swap 'upper' for 'lower'.
>
> Gregory
>
>
>
>
> ---------------------------------
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list