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

  • $\begingroup$ No, but trivial to build fn. to do so... $\endgroup$
    – ciao
    Commented Mar 16, 2015 at 21:52
  • $\begingroup$ 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$. $\endgroup$ Commented Mar 16, 2015 at 22:11
  • $\begingroup$ 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. $\endgroup$
    – David
    Commented Mar 16, 2015 at 22:23

1 Answer 1


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]

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}*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.