sprand — Create sparse random matrices.
sprand returns a sparse random matrix R, with double output by default and an explicit single-precision form. The sprand(S) form uses S as the input sparsity pattern, and dimension-based forms support 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
S- Type
NumericArray- Required
- Yes
- Description
- Full or sparse matrix supplying the nonzero pattern.
typename- Type
StringScalar- Required
- No
- Default
double- Description
- Sparse storage type: double or single.
m- Type
SizeArg- Required
- Yes
- Description
- Number of rows.
n- Type
SizeArg- Required
- Yes
- Description
- Number of columns.
density- Type
NumericScalar- Required
- Yes
- Description
- Approximate density in [0,1].
rc- Type
NumericArray- Required
- Yes
- Description
- Reciprocal condition number target or singular-value vector of length at most min(m,n).
Returns
R- Type
NumericArray- Description
- Sparse result matrix.
Errors
RunMat:sparse:InvalidInput- When
- Inputs are not a supported sparse construction form.
- Message
sparse: invalid input
RunMat:sparse:InvalidIndex- When
- Row or column subscripts are nonpositive, noninteger, or outside explicit dimensions.
- Message
sparse: invalid index
RunMat:sparse:Internal- When
- Sparse matrix materialisation fails internally.
- Message
sparse: internal error
Behavior
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] whose length is at most min(m,n). RunMat starts from the requested singular profile and applies random orthogonal plane rotations to add sparse fill while preserving that profile.
A trailing "double" or "single" typename selects the sparse output class.
All eight integer classes are documented for the pattern input S. Typed integer dimensions, density, and condition controls are RunMat extensions; dimensions are structural, while numerical controls reject if binary64 conversion would round.
Large requested sparse patterns, condition-profile dense intermediates, and excessive plane-rotation work are rejected before allocating or running unbounded work.
Execution and residency
sprand runs on the host RNG and produces a host sparse matrix.
sprand uses RunMat's host RNG and returns host-resident sparse CSC storage.
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 = 1Using 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.
Related Array functions
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how sprand is executed, line by line, in Rust.
- View the source for sprand in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.