# Generating a random singular matrices using matlab

Could anybody tell me that How one can generate a random singular matrices using matlab? I know that using rand(n) we can generate a random matrix of order n. But I found that these random matrices are non singular while I am interested in generating random singular matrices of higher order. Is there any command through which we can generate a random singular matrices? I need help.

Thanks a lot

-

If you're not too worried about the distribution of the matrix, you could just generate an $n-1 \times n$ matrix, and let the $n$th row be the sum of the others.

n = 3;
A = rand(n-1,n);
A(end+1,:) = sum(A);

-
A(end,:) refers to the last row of the matrix $A$. Assigning a row to row end+1 is a quick way to append a row to the matrix. –  Jacob Sep 16 '12 at 8:26

Another possibility is to take a random $n \times n$ matrix and adjust one entry to make the determinant $0$. This will be possible as long as the corresponding cofactor is not $0$, which is almost always the case.

-
Thank you very much sir. I have to think how to make code for that? –  srijan Sep 16 '12 at 6:50

How are the entries of the matrix generated? Are they Bernoulli / Gaussian / uniform random variables? Whatever the PDF of each matrix entry, here's what you can do:

1. Generate a random matrix.
2. Check if matrix is singular.
3. If singular, then use it. If not singular, discard it, and go back to 1.

Of course, you have to think about what "singular" means in MATLAB, for we're using floating-point numbers, not real numbers. Rank lower than eps would do, but it may be too conservative. You would wait a billion years until you found a singular random matrix.

-
entries of the matrices are uniformally generated. I was reading a research paper where author has taken numerical experiments performed on random singular matrices. I thought there must be some particular code but now I feel he might have taken rectangular singular matrices –  srijan Sep 16 '12 at 7:01
Thanks for replying me. –  srijan Sep 16 '12 at 7:01
@srijan: Is matrix singularity a concept that is applicable to rectangular matrices? Besides, if you're using uniform random variables and floating-point arithmetic, the probability of obtaining two linearly dependent rows or columns appears to be vanishingly small. –  Rod Carvalho Sep 16 '12 at 7:06
You are taking me wrong. I don't mean there doesn't exist square singular matrices. As for as I know that a real random matrix has full rank with probability 1 and a rational random matrix has full rank with probability 1 too. Thats why i concluded so. I am sorry for that.:( –  srijan Sep 16 '12 at 7:13
@srijan: Are the entries of the matrix independent random variables? Vectorize the $n \times n$ matrix and obtain a $n^2$ random vector. What is the correlation matrix of this random vector? –  Rod Carvalho Sep 16 '12 at 7:22
show 1 more comment

If the distribution of the points is not important, you can generate an $n\times n$ matrix with rank $k$ with the following pseudocode.

Generate a random matrix $R$ of dimension $n\times k$.
Set A=R*R^T.


Then $A$ will be an $n\times n$ matrix with rank $k$. This is due to the fact that $A$ is a series of $k$ outer products from the columns of $R$.

-
Thanks daryl... –  srijan Sep 16 '12 at 9:14