Skip to content

sprand

Sparse uniform random matrix.

S = sprand(m, n, density)
S = sprand(m, n, density, rc)
S = sprand(A)

Returns an m × n sparse matrix of uniform-random values in [0, 1] with approximately density * m * n non-zero entries (density is a fraction in [0, 1]).

With the optional rc, controls the reciprocal of the condition number — used to generate test matrices with a known condition number.

With a single sparse argument, returns a new sparse matrix with the same sparsity pattern as A but uniform-random values where A has non-zeros.

sprand(100, 100, 0.05) % ~500 non-zero entries
sprand(speye(5)) % keep diagonal pattern, replace 1s with random
  • sprandn — Sparse standard-normal random matrix.
  • rand — Uniformly distributed random numbers in [0, 1].
  • sparse — Create a sparse matrix.