RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • 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
    • sprand
    • true
    • uint16.empty
    • uint32.empty
    • uint64.empty
    • uint8.empty
    • zeros

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).
NameTypeRequiredDefaultDescription
SNumericArrayYes—Full or sparse matrix supplying the nonzero pattern.
typenameStringScalarNodoubleSparse storage type: double or single.
mSizeArgYes—Number of rows.
nSizeArgYes—Number of columns.
densityNumericScalarYes—Approximate density in [0,1].
rcNumericArrayYes—Reciprocal condition number target or singular-value vector of length at most min(m,n).

Returns

R
Type
NumericArray
Description
Sparse result matrix.
NameTypeDescription
RNumericArraySparse 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
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

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 = 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.

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

accumarray

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

combinations · nchoosek · perms

Binning

discretize

Indexing

find · ind2sub · sub2ind

Grouping

findgroups · groupcounts · grp2idx · splitapply

Introspection

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

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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Behavior
  • Execution and residency
  • Example
  • Random values on an existing pattern
  • Using sprand with coding agents
  • FAQ
  • Related Array functions
  • Creation
  • Accumulation
  • Sorting Sets
  • Shape
  • Combinatorics
  • Binning
  • Indexing
  • Grouping
  • Introspection
  • Source you can read
  • About RunMat