Search code examples
rmatrixdiagonalsymmetric

Constructing symmetric matrix on R


I want to construct a 1000x1000 symmetric matrix where all diagonal elements are 1 and others are 0.5. I don't know how to do this for such a large matrix. Can someone help me out here?

Thank you!


Solution

  • m <- matrix(0.5, ncol = 1000, nrow = 1000)
    diag(m) <- 1