# Generation of 'n' random symmetric matrices

I want to generate 3 random symmetric matrices, each of dimension 3 whose elements are normally distributed. It could be done simply as

A = RandomVariate[NormalDistribution[0, 1], {3, 3}]
B = RandomVariate[NormalDistribution[0, 1], {3, 3}]
C = RandomVariate[NormalDistribution[0, 1], {3, 3}]
R1 =(A + Transpose[A])/2
R2 =(B + Transpose[B])/2
R3 =(C + Transpose[C])/2


I am looking for other ways in which the same problem can be done for the case where number of matrices is large.

• If you have a recent version of Mathematica, there are a lot of built-in distributions for random matrices. See reference.wolfram.com/language/guide/MatrixDistributions.html for more. – Pillsy Apr 28 '17 at 19:46
• I can't see any problem with what you are doing - do you want a simple function that generates a random matrix? – mikado Apr 28 '17 at 19:50
• @mikado I want to generate 1000 random symmetric matrices for which using this code isn't a good idea. I am looking for better algorithm or a function to do so. – NerdySnail Apr 28 '17 at 19:56
• @NerdySnail Do you want just a few matrices (so you can hand pick the names) or a whole slew of them? – Igor Rivin Apr 29 '17 at 15:43
• Let's say we have 100 of them. Handpicking would be absurd. – NerdySnail Apr 29 '17 at 16:26

## 2 Answers

Firstly, your method does not work. The diagonal elements are $\mathcal{N}(0, 1),$ the off-diagonal elements are $\mathcal{N}(0, \frac{1}{\sqrt{2}}).$ Secondly, GaussianOrthogonalMatrixDistribution[\[Sigma],n] does work (in your case, $\sigma = 1, n=3.$

EDIT as pointed out by Mikey, the mathematica command does the wrong thing. However, this does:

randomSymMat =
Module[{mat = RandomVariate[#, {#2, #2}], upper, diag},
upper = UpperTriangularize[mat, 1];
diag = DiagonalMatrix[Diagonal@mat];
diag + upper + Transpose[upper]] &;


so

randomSymMat[NormalDistribution[0, 1], #]& /@ Array[3&, 1000]


Will do what you need.

• hmmm...reading the docs, GaussianOrthogonalMatrixDistribution[] is a distribution of a symmetric matrix (x+x^T)/2, where x is a square matrix with independent identically distributed matrix elements that follow NormalDistribution[0,[Sigma]]. Sure sounds like x is distributed same as A in his question, no? – MikeY Apr 28 '17 at 20:19
• @Igor,Indeed.As Pillsy already mentions,I should have used MatrixDistributions. However doing that too,doesn't lead us closer to the actual problem of generating a thousand of such matrices in a go. – NerdySnail Apr 28 '17 at 20:21
• @MikeY That is NOT the Gaussian Orthogonal Matrix Distribution, so either the name is wrong or the docs are wrong. – Igor Rivin Apr 28 '17 at 20:22
• @MikeY The docs are NOT wrong. This is really messed up. – Igor Rivin Apr 28 '17 at 20:25
• @IgorRivin You can report the problem at the Give Feedback link here: reference.wolfram.com/language/ref/… – Alan Apr 28 '17 at 21:17

OK, here's my shot at it.

makeMat[eps_, n_] := (res = RandomVariate[NormalDistribution[0, eps], {n, n}];
UpperTriangularize[res] + Transpose@UpperTriangularize[res, 1])

makeMat[1,3]


$$\left( \begin{array}{ccc} 0.733676 & 0.0102509 & -0.35534 \\ 0.0102509 & -0.462317 & -0.132434 \\ -0.35534 & -0.132434 & 0.89037 \\ \end{array} \right)$$