sprand — Create sparse random matrices.

sprand creates sparse double matrices with random values and optional reciprocal-condition control.

Syntax

R = sprand(S)
R = sprand(S, typename)
R = sprand(m, n, density)
R = sprand(m, n, density, typename)
R = sprand(m, n, density, rc)
R = sprand(m, n, density, rc, typename)

Inputs

NameTypeRequiredDefaultDescription
ANumericArrayYesFull or sparse matrix.
SNumericArrayYesFull or sparse matrix.
typenameStringScalarNodoubleSparse storage type. RunMat currently supports double sparse storage.
mSizeArgYesNumber of rows.
nSizeArgYesNumber of columns.
densityNumericScalarYesApproximate density in [0,1].
rcNumericArrayYesReciprocal condition number target or singular-value vector.

Returns

NameTypeDescription
SNumericArraySparse double matrix.

Errors

IdentifierWhenMessage
RunMat:sparse:InvalidInputInputs are not a supported sparse construction form.sparse: invalid input
RunMat:sparse:InvalidIndexRow or column subscripts are nonpositive, noninteger, or outside explicit dimensions.sparse: invalid index
RunMat:sparse:InternalSparse matrix materialisation fails internally.sparse: internal error

How sprand works

  • sprand(S) preserves the nonzero pattern and shape of S while replacing stored values with uniform random values.
  • sprand(m, n, density) creates an m x n sparse matrix with approximately round(m*n*density) stored entries.
  • density must be finite and between 0 and 1.
  • sprand(m, n, density, rc) accepts a scalar reciprocal-condition target or a vector of singular values in [0,1]. RunMat starts from the requested singular profile and applies random orthogonal plane rotations to add sparse fill while preserving that profile.
  • A trailing "double" typename is accepted for MATLAB compatibility. "single" sparse storage is rejected until RunMat has native single-precision sparse matrices.
  • Large requested sparse patterns, condition-profile dense intermediates, and excessive plane-rotation work are rejected before allocating or running unbounded work.

GPU memory and residency

sprand runs on the host RNG and produces a host sparse matrix.

Example

Random values on an existing pattern

P = sparse([1; 3], [1; 2], [10; 20], 3, 2);
R = sprand(P);
issparse(R)

Expected output:

ans = 1

Using sprand with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how sprand changes the result.

Run a small sprand example, explain the result, then change one input and compare the output.

FAQ

Does sprand(...,rc) enforce the reciprocal condition number?

Yes. RunMat uses orthogonal row and column plane rotations, which preserve the singular values of the requested scalar reciprocal condition target or vector singular profile. The requested density remains approximate for small or structurally constrained shapes.

Creation

colon · eye · false · fill · full · inf · linspace · logspace · magic · meshgrid · nan · nchoosek · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · true · zeros

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat

Indexing

find · ind2sub · sub2ind

Introspection

iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how sprand is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.