Skip to content

Help creating a symmetric matrix?

6 messages · Matt Considine, Rui Barradas, Sarah Goslee +1 more

1 day later
#
Matt Considine wrote
Hello,

Aren't you complicating?

In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
not the vector?

z<-diag(6)
ind <- lower.tri(z)
z[ind] <- v                        #This works
z

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/Help-creating-a-symmetric-matrix-tp4227301p4230335.html
Sent from the R help mailing list archive at Nabble.com.
#
Or the slightly shorter:

z<-diag(6)
z[row(z) > col(z)] <- v

which is what lower.tri() does,

and
z <- diag(6)
z[lower.tri(z)] <- v

also works.

Sarah
On Fri, Dec 23, 2011 at 9:31 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:

  
    
#
Dear Matt, Sarah and Rui,

To answer the original question for creating a symmetric matrix
z<-diag(6)
z[row(z) > col(z)] <- v
z <- z + t(z)
diag(z) <- 0
[,1]    [,2]    [,3]    [,4]    [,5]    [,6]
[1,] 0.00000 0.33740 0.26657 0.23388 0.23122 0.21476
[2,] 0.33740 0.00000 0.20829 0.20486 0.19439 0.19237
[3,] 0.26657 0.20829 0.00000 0.18633 0.17298 0.17174
[4,] 0.23388 0.20486 0.18633 0.00000 0.16822 0.16480
[5,] 0.23122 0.19439 0.17298 0.16822 0.00000 0.15027
[6,] 0.21476 0.19237 0.17174 0.16480 0.15027 0.00000


Bill
On Dec 24, 2011, at 6:04 AM, Sarah Goslee wrote:

            
William Revelle		           http://personality-project.org/revelle.html
Professor			           http://personality-project.org
Department of Psychology   http://www.wcas.northwestern.edu/psych/
Northwestern University	   http://www.northwestern.edu/
Use R for psychology             http://personality-project.org/r
It is 6 minutes to midnight	   http://www.thebulletin.org
#
On Sat, Dec 24, 2011 at 8:38 AM, William Revelle <lists at revelle.net> wrote:
I read the original question as *only* wanting the complete lower
triangle, with diagonal of 1 and 0 in the upper triangle.

If your interpretation is correct, there's also this convenience function:

library(ecodist)
z <- full(v)

Sarah
#
Thank you all for your help and best wishes for the holiday season.
Matt Considine
On 12/24/2011 8:38 AM, William Revelle wrote: