# Generation of random Matrix with Real eigen values

does anyone know any matlab algorithm that can help me generate a random matrix with REAL EIGEN values? Thanks.

• What is your definitions of "random matrix"? If you want some distribution over all matrices with real eigenvalues this is hard. If you want a distribution over all possible real eigenvalues you could do something as simple as a diagonal matrix with random entries on the diagonal. – Doug Lipinski Sep 27 '15 at 1:26

You don't mention what kind of distribution you want for the entries of the random matrix, so the simplest recipe that I can think of is to first create a random $n \times n$ matrix $\mathtt{A}$ and to then obtain $\mathtt{B} = \frac{1}{2} ( \mathtt{A} + \mathtt{A}^{\mathtt{T}} )$. That gives you a symmetric matrix $\mathtt{B}$ with random entries. Since symmetric matrices have real eigenvalues, the matrix $\mathtt{B}$ will satisfy your requirement.

Matlab code:

A = rand(n)
B = 0.5 * (A + transpose(A))


Here's another approach, building on Bill Barth's answer. First, create a random diagonal matrix $\mathtt{D}$, then obtain a random orthogonal matrix $\mathtt{Q}$ and, finally, compute the similarity transformation $\mathtt{B} = \mathtt{Q} \, \mathtt{D} \, \mathtt{Q}^{\mathtt{T}}$. The matrix $\mathtt{B}$ will have the same eigenvalues as $\mathtt{D}$ but will not be diagonal in general. A simple way to come up with a random orthogonal matrix $\mathtt{Q}$ is to compute the QR decomposition of a random matrix.

Matlab code:

D = diag(rand(n, 1))
[Q ~] = qr(rand(n))
B = Q * D * transpose(Q)

• Wonderful. I think my problem has been solved by your answer. You guys are too awesome. Thanks – user1144656 Sep 27 '15 at 1:32
• Which is the answer I should have been able to come up with. Nice. – Bill Barth Sep 27 '15 at 2:57
• @user1144656 If this answer solved your problem, you should accept it! – Christian Clason Oct 3 '15 at 21:25

If you want to specify the eigenvalues, then you can just put them on the diagonal of an otherwise empty matrix.