7

I would like to generate a random real symmetric square matrix with entries uniformly distributed between 0 and 1. My attempt is: a = rand(5); b = a + a.'

My worry is that whilst matrix a is uniformly distributed according to the documentation http://www.mathworks.com.au/help/techdoc/ref/rand.html matrix b might not be since the average of two random numbers might not be the same as the original number.

I tried to use hist(a); hist(b) but not sure how to interpret the resulting graph. EDIT: According to Oli matrix b is no longer uniformly distributed, is there a way to make it that way?

Aina
  • 653
  • 2
  • 9
  • 22

2 Answers2

15

No, if you do that then b will not be uniformly distributed; it will have a triangular distribution.

How about something like this:

a = rand(5);
b = triu(a) + triu(a,1)';

where triu() takes the upper-triangular part of the matrix.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • thanks, Oli, any advice on how to make it uniformly distributed? – Aina Mar 17 '12 at 13:46
  • thanks, Oli, so using triu function keeps the uniform distribution? I have just run it and hist(b) looks different to what I had for my matrix b. – Aina Mar 17 '12 at 13:53
  • @Aina: This approach avoids adding any elements together. – Oliver Charlesworth Mar 17 '12 at 13:53
  • @Aina: 25 elements is not enough to produce a useful histogram. You should generate, say, 10000 of these random matrices, and then do a histogram on the whole thing. – Oliver Charlesworth Mar 17 '12 at 14:03
  • thanks, Oli, just tried to create a 1000x1000 matrix and then compared its histogram to the histogram of matrix b created using your method. Looks exactly the same! Thanks, again! – Aina Mar 17 '12 at 14:12
2

You can only get uniformly distributed entries on half of the matrix.

a=rand(5);
b=triu(a).'+triu(a,1);
  • thanks g24l, so then it is IMPOSSIBLE to have a random symmetric matrix with uniformly distributed entries? – Aina Mar 17 '12 at 13:58
  • @Aina : I was talking on how to generate the matrix. The elements of the matrix are uniformly distributed, but not random, and not iid. –  Mar 17 '12 at 14:38