## Friday, April 6, 2012

### Generate random matrices with a given condition number (or spectrum)

It is really easy to perform this task. Just generate a random matrix, for example, in Matlab:
n=4
A=rand(n)
then we need no extract the (random) basis with the QR decomposition.
[Q,R]=qr(A)
and then we fix the spectrum. The best possibly conditioned matrix would have a condition number of one. Therefore we can achieve that with
D=diag(ones(n,1))
M=Q*D*Q'
This would produce a normal matrix $MM^T = M^T M$. If we want a non-normal matrix, we just need another random basis, that we can take from another random matrix $B$.
B=rand(n)
[V,R]=qr(A)
M=Q*D*V'
If we want to fix only the first (largest) eigenvalue to one, but leaving a random spectrum, we generate a random sequence
sp=randn(n,1)
sp=sp/max(sp)
D=diag(sp)
M=Q*D*V'