How to generate symmetric matrices in Julia?

Viewed 110

Functions like rand(m,n) generate a random matrix of m rows and n columns. Is there any function in Julia that can generate a Symmetric matrix of arbitrary dimensions?

1 Answers

You can create a symmetric matrix as you do in rand(m,n), but you will not be able to assign to non-diagonal elements as that might break its symmetry. So, you should create the general matrix first then convert to symmetric.

Symmetric(rand(0:9,5,5))
5×5 Symmetric{Int64, Matrix{Int64}}:
 5  2  1  4  1
 2  1  6  8  0
 1  6  2  0  6
 4  8  0  7  1
 1  0  6  1  4
Related