[R] Need help on upper.tri()

Nikhil Kaza nikhil.list at gmail.com
Tue Aug 3 17:55:34 CEST 2010


try using Matrix package instead

mat <- Matrix(rnorm(25),5,5)
forceSymmetric(mat)

The reason your method does not work is because matrix is effectively  
a vector and the indices increase along rows within a column.

Nikhil

On Aug 3, 2010, at 7:36 AM, Ron Michael wrote:

> HI, I am really messing up to make a symmetrical matrix using  
> upper.tri() & lower.tri() function. Here is my code:
>
>> set.seed(1)
>> mat = matrix(rnorm(25), 5, 5)
>> mat
>            [,1]       [,2]       [,3]        [,4]        [,5]
> [1,] -0.6264538 -0.8204684  1.5117812 -0.04493361  0.91897737
> [2,]  0.1836433  0.4874291  0.3898432 -0.01619026  0.78213630
> [3,] -0.8356286  0.7383247 -0.6212406  0.94383621  0.07456498
> [4,]  1.5952808  0.5757814 -2.2146999  0.82122120 -1.98935170
> [5,]  0.3295078 -0.3053884  1.1249309  0.59390132  0.61982575
>> mat[lower.tri(mat)] = mat[upper.tri(mat)]
>> mat
>             [,1]        [,2]        [,3]        [,4]        [,5]
> [1,] -0.62645381 -0.82046838  1.51178117 -0.04493361  0.91897737
> [2,] -0.82046838  0.48742905  0.38984324 -0.01619026  0.78213630
> [3,]  1.51178117 -0.01619026 -0.62124058  0.94383621  0.07456498
> [4,]  0.38984324  0.94383621  0.78213630  0.82122120 -1.98935170
> [5,] -0.04493361  0.91897737  0.07456498 -1.98935170  0.61982575
>
>
> Which is not coming as symmetrical function. Can anyone point me on  
> the correct way of using upper, lower.try() function to get a  
> symmetrical matrix?
>
> Thanks,
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list