Just to complete, I would also like to show this technique. Adding transposition will not work if the lower part of the matrix (below the diagonal) has filled values, since it will add them to the upper part of the matrix.
Using the Matrix package, we can create a sparse matrix, which in the case of creating a symmetric large matrix will require much less memory and even speed it up.
To create a symmetric sparse matrix from the matrix e , we would do:
library(Matrix) rowscols <- which(upper.tri(e), arr.ind=TRUE) sparseMatrix(i=rowscols[,1], #rows to fill in j=rowscols[,2], #cols to fill in x=e[upper.tri(e)], #values to use (ie the upper values of e) symmetric=TRUE, #make it symmetric dims=c(nrow(e),nrow(e))) #dimensions
Output:
5 x 5 sparse Matrix of class "dsCMatrix" [1,] . 2 3 4 5 [2,] 2 . 6 8 10 [3,] 3 6 . 12 15 [4,] 4 8 12 . 20 [5,] 5 10 15 20 .
Microbenchmark:
Let me make a function to make a symmetric matrix from a matrix (by default it copies the top of the matrix to the bottom):
symmetrise <- function(mat){ rowscols <- which(upper.tri(mat), arr.ind=TRUE) sparseMatrix(i=rowscols[,1], j=rowscols[,2], x=mat[upper.tri(mat)], symmetric=TRUE, dims=c(nrow(mat),ncol(mat)) ) }
And the test:
> microbenchmark( e + t(e), symmetrise(e), e[lower.tri(e)] <- t(e)[lower.tri(e)], times=1000 ) Unit: microseconds expr min lq mean median uq max neval cld e + t(e) 75.946 99.038 117.1984 110.841 134.9590 246.825 1000 a symmetrise(e) 5530.212 6246.569 6950.7681 6921.873 7034.2525 48662.989 1000 c e[lower.tri(e)] <- t(e)[lower.tri(e)] 261.193 322.771 430.4479 349.968 395.3815 36873.894 1000 b
As you can see, symmetrise is actually much slower than e + t(e) or df[lower.tri(df)] <- t(df)[lower.tri(df)] , but at least you there is a function that automatically balances the matrix (takes the top and creates a default by default), and if you have a large matrix where memory is a problem, this can come in handy.
PS Wherever you find . in the matrix, this means zero. When using another system, a sparse matrix is ββa kind of βcompressedβ object, which makes it more efficient from a memory point of view.