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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Full or sparse matrix. |
S | NumericArray | Yes | — | Full or sparse matrix. |
typename | StringScalar | No | double | Sparse storage type. RunMat currently supports double sparse storage. |
m | SizeArg | Yes | — | Number of rows. |
n | SizeArg | Yes | — | Number of columns. |
density | NumericScalar | Yes | — | Approximate density in [0,1]. |
rc | NumericArray | Yes | — | Reciprocal condition number target or singular-value vector. |
Returns
| Name | Type | Description |
|---|---|---|
S | NumericArray | Sparse double matrix. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:sparse:InvalidInput | Inputs are not a supported sparse construction form. | sparse: invalid input |
RunMat:sparse:InvalidIndex | Row or column subscripts are nonpositive, noninteger, or outside explicit dimensions. | sparse: invalid index |
RunMat:sparse:Internal | Sparse matrix materialisation fails internally. | sparse: internal error |
How sprand works
sprand(S)preserves the nonzero pattern and shape ofSwhile replacing stored values with uniform random values.sprand(m, n, density)creates anm x nsparse matrix with approximatelyround(m*n*density)stored entries.densitymust be finite and between0and1.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 = 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 · 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
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. 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 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.