Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate symmetric random matrix?

I want to generate a random matrix which should be symmetric.

I have tried this:

matrix(sample(0:1, 25, TRUE), 5, 5)

but it is not necessarily symmetric.

How can I do that?

like image 436
Majid Avatar asked Nov 14 '14 19:11

Majid


People also ask

How do you show a matrix is a symmetric matrix?

A matrix is symmetric if and only if it is equal to its transpose. All entries above the main diagonal of a symmetric matrix are reflected into equal entries below the diagonal. A matrix is skew-symmetric if and only if it is the opposite of its transpose. All main diagonal entries of a skew-symmetric matrix are zero.


1 Answers

Another quite interesting opportunity is based on the following mathematical fact: if A is some matrix, then A multiplied by its transpose is always symmetric.

> A <- matrix(runif(25), 5, 5)
> A %*% t(A)
         [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 1.727769 1.0337816 1.2195505 1.4661507 1.1041355
[2,] 1.033782 1.0037048 0.7368944 0.9073632 0.7643080
[3,] 1.219551 0.7368944 1.8383986 1.3309980 0.9867812
[4,] 1.466151 0.9073632 1.3309980 1.3845322 1.0034140
[5,] 1.104135 0.7643080 0.9867812 1.0034140 0.9376534
like image 64
tonytonov Avatar answered Sep 23 '22 21:09

tonytonov