# Creating a random make matrix with a particular rank

Does Mathematica have a built-in function that will return a random mxn matrix with rank r?l

• No, but trivial to build fn. to do so...
– ciao
Commented Mar 16, 2015 at 21:52
• Create an $r \times r$ rank-$r$ matrix, then pad each column with $m-r$ with the same random values (thus preserving the rank of the matrix). Then create $n-r$ column vectors by a random linear combination of the $r$ basis vectors. You can randomly permute columns or make new linear combinations, to "scramble" the matrix without changing its rank $r$. Commented Mar 16, 2015 at 22:11
• I did this: r = 0; While[r != 3, A = RandomInteger[5, {4, 5}]; r = MatrixRank[A]], but I will check out your answer as well. Thanks. Commented Mar 16, 2015 at 22:23

### For square matrices:

You can try this:

RandomMatrix[rank_, m_] :=
Sum[TensorProduct @@ RandomReal[{-1, 1}, {2, m}], {i, rank}];


It returns a pseudorandom m-by-m matrix with rank rank.

Example usage:

MatrixRank@RandomMatrix[5, 10]
(*5*)


### Rectangular matrices

For rectangular matrices, try this:

RandomMatrix[rank_, m_, n_] :=
Sum[RandomReal[{-1, 1}, m]\[TensorProduct]RandomReal[{-1, 1},
n], {i, rank}];


Example usage:

Dimensions[RandomMatrix[3, 4, 5]]
MatrixRank[RandomMatrix[3, 4, 5]]
(*{4, 5}*)
(*3*)