# How to generate 1000 symmetric matrices with defined letter?

I am just trying to create many number of random 3x3 matrices in the array (-100,100) for a statistical research and I am stuck to create symmetric many number of matrices. And also those matrices should be defined by letters, because after generation, all of those matrices place in an equation.

h[n_] := Module[{m}, m = RandomReal[{-100, 100}, {3, 3}];
m SparseArray[{i_, j_} /; i >= j -> 1, {3, 3}] +
Transpose[m SparseArray[{i_, j_} /; i > j -> 1, {3, 3}]] // Normal]
h[3] // MatrixForm
For[n = 0, n < 1000, n++, Print[h[n] // MatrixForm]]


with these code I can create 3x3 symmetric 1000 matrices, but how can I define these matrices as stable. I mean for example, for this code when I print h[7], everytime it gives another random matrix. How can I store those 1000 matrices for variables of h (h[n]) (I want stable matrices When I print any value of h[n]) ?

• add SeedRandom[1];  before m = RandomReal[...];
– kglr
Commented Dec 25, 2017 at 12:37
• When I do that, every matrices give same solution for example h[1] and h[2] give the same matrix :/ Commented Dec 25, 2017 at 13:02

This creates 1000 symmetric random 3-by-3 matrices at once.

m = RandomReal[{-100, 100}, {1000, 3, 3}];
Do[m[[All, i, j]] = m[[All, j, i]], {i, 1, 3}, {j, i + 1, 3}];


You can reference the n-th matrix with m[[n]]. Note the double brackets as m is a three dimensional array. If you insist on single brackets then define

h[n_]:=m[[n]]

• Thank you! Thats exactly what I want, I really appriciate that :) Commented Dec 25, 2017 at 13:08
• You're welcome! Commented Dec 25, 2017 at 13:18
letter = Alphabet[]

h[n_] := Module[{m}, SeedRandom[n]; m = RandomChoice[letter, {3, 3}];
m SparseArray[{i_, j_} /; i >= j -> 1, {3, 3}] +
Transpose[m SparseArray[{i_, j_} /; i > j -> 1, {3, 3}]] // Normal]

h[3] // MatrixForm

For[n = 0, n < 1000, n++, Print[h[n] // MatrixForm]]


{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}

$\left( \begin{array}{ccc} \text{p} & \text{i} & \text{v} \\ \text{i} & \text{b} & \text{o} \\ \text{v} & \text{o} & \text{s} \\ \end{array} \right)$

... 1000 matrices ...

If you want different set use a different seed, for example, SeedRandom[n+3] or SeedRandom[n+17].

• You get me wrong I think I just want to create matrices for each value of matrices should be array of -100,100 but I Just define those matrices. with letter for example h[1] or h[2] or so on, I created this matrices but I cant store it with variables Commented Dec 25, 2017 at 12:57