# [R] Help creating a symmetric matrix?

Sarah Goslee sarah.goslee at gmail.com
Sat Dec 24 14:48:14 CET 2011

```On Sat, Dec 24, 2011 at 8:38 AM, William Revelle <lists at revelle.net> wrote:
> Dear Matt, Sarah and Rui,
>
> To answer the original question for creating a symmetric matrix

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

>
>>>> 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?
>>>>
>>>> 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
>>>