[R] Help creating a symmetric matrix?

William Revelle lists at revelle.net
Sat Dec 24 14:38:06 CET 2011


Dear Matt, Sarah and Rui,

To answer the original question for creating a symmetric matrix 


>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>> 0.19439, 0.19237,
>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)


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

> z
        [,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:

> 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:
>> 
>> Matt Considine wrote
>>> 
>>> Hi,
>>> I am trying to work with the output of the MINE analysis routine found at
>>>    http://www.exploredata.net
>>> 
>>> Specifically, I am trying to read the results into a matrix (ideally an
>>> n x n x 6 matrix, but I'll settle right now for getting one column into
>>> a matrix.)
>>> 
>>> The problem I have is not knowing how to take what amounts to being one
>>> half of a symmetric matrix - excluding the diagonal - and getting it
>>> into a matrix.  I have tried using "lower.tri" as found here
>>>    https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
>>> but it appears to only partially fill in the matrix.  My code and an
>>> example of the output is below.  Can anyone point me to an example that
>>> shows how to create a matrix with this sort of input?
>>> 
>>> Thank you in advance,
>>> Matt
>>> 
>>> #v<-newx[,3]
>>> #or, for the sake of this example
>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>> 0.19439, 0.19237,
>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>>> z<-diag(6)
>>> ind <- lower.tri(z)
>>> z[ind] <- t(v)[ind]
>>> 
>>> z
>>>          [,1]    [,2] [,3] [,4] [,5] [,6]
>>> [1,] 1.00000 0.00000    0    0    0    0
>>> [2,] 0.26657 1.00000    0    0    0    0
>>> [3,] 0.23388 0.19237    1    0    0    0
>>> [4,] 0.23122 0.18633   NA    1    0    0
>>> [5,] 0.21476 0.17298   NA   NA    1    0
>>> [6,] 0.20829 0.17174   NA   NA   NA    1
>>> 
>>> 
>> 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
>> 
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
> 
> ______________________________________________
> 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.
> 

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



More information about the R-help mailing list