RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

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.

LicensePrivacy
/
See all docs
Builtin Reference
    • colon
    • createArray
    • empty
    • eye
    • false
    • full
    • inf
    • linspace
    • logspace
    • magic
    • meshgrid
    • nan
    • nchoosek
    • ndgrid
    • nonzeros
    • ones
    • peaks
    • perms
    • rand
    • randi
    • randn
    • randperm
    • range
    • sparse
    • spdiags
    • speye
    • spones
    • sprand
    • true
    • zeros

sparse — Create host-resident sparse matrices from full arrays, sizes, or row/column/value triplets.

sparse creates a host-resident compressed sparse column matrix. MATLAB-compatible forms construct double, single, or logical sparse values as documented; runmat compatibility mode additionally supports exact sparse storage for all eight integer classes.

Syntax

S = sparse(A)
S = sparse(m, n)
S = sparse(m, n, typename)
S = sparse(i, j, v)
S = sparse(i, j, v, m, n)
S = sparse(i, j, v, m, n, nzmax)

Inputs

NameTypeRequiredDefaultDescription
ANumericArrayYes—Full or sparse matrix to convert.
mSizeArgYes—Number of rows.
nSizeArgYes—Number of columns.
typenameStringScalarYes—Sparse storage type: double or single.
iNumericArrayYes—One-based row subscripts.
jNumericArrayYes—One-based column subscripts.
vNumericArrayYes—Values for each row/column pair.
nzmaxSizeArgNo—Allocation hint accepted for MATLAB compatibility.

Returns

NameTypeDescription
SNumericArraySparse 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 sparse works

  • sparse(A) stores only nonzero values and preserves documented double, single, or logical input class. Integer A is an exact RunMat-only extension.
  • sparse(m, n) returns an m x n sparse double matrix with zero stored entries.
  • sparse(i, j, v) creates a sparse matrix whose size is inferred from the largest row and column subscripts.
  • sparse(i, j, v, m, n) creates an m x n sparse matrix and errors if any subscript falls outside those dimensions.
  • sparse(i, j, v, m, n, nzmax) accepts the MATLAB allocation hint for compatibility; RunMat's storage is still sized from the produced nonzero entries.
  • Duplicate (i, j) entries are summed. Entries whose final value is zero are not stored.
  • Row and column subscripts are one-based positive integers. All eight integer classes are accepted for i and j; when both are integer arrays they must use the same datatype and are parsed without a double round trip.
  • Integer triplet values retain their exact class in runmat compatibility mode. Duplicate integer values use class-saturating addition; MATLAB-compatible modes reject the integer-value extension before construction.
  • Sparse matrices support scalar row/column indexing and linear indexing; scalar selections return sparse 1 x 1 values, unstored entries read as zero, and linear indices are resolved in column-major order.
  • Sparse slice indexing preserves sparse storage for non-scalar selections.
  • Sparse indexed assignment currently raises RunMat:SparseAssignmentUnsupported instead of silently densifying or partially mutating storage.
  • Sparse real matrices interoperate with +, -, and .* for sparse-sparse, sparse-dense, dense-sparse, sparse-scalar, character, logical, and complex operands. Addition and subtraction with dense, complex, or nonzero scalar operands return full storage when unstored sparse zeros become nonzero; sparse-preserving real products and sparse-sparse sums/differences return sparse storage.

Does RunMat run sparse on the GPU?

The GPU metadata for sparse is intentionally marked as gather-immediate. This makes the representation transition explicit to the planner and prevents pretending that dense GPU buffers are native sparse matrices.

GPU memory and residency

Sparse values are currently host-resident. Passing a GPU tensor to sparse triggers a gather, after which RunMat builds a compressed sparse column matrix on the host.

Examples

Creating a sparse matrix from triplets

S = sparse([1; 3; 2], [1; 2; 3], [10; 20; 30], 3, 3);
nnz(S)

Expected output:

ans = 3

Summing duplicate row and column pairs

S = sparse([1; 1; 2], [2; 2; 3], [4; 5; 6], 2, 3);
[r, c, v] = find(S);
v

Expected output:

v = [9; 6]

Converting a full matrix

A = [0 5; 7 0];
S = sparse(A);
size(S)

Expected output:

ans = [2 2]

Creating an empty sparse matrix

S = sparse(4, 5);
nnz(S)

Expected output:

ans = 0

Reading stored and unstored entries

S = sparse([1; 2], [1; 3], [10; 23], 3, 3);
a = full(S(1,1));
b = full(S(2,1));
c = full(S(8));
[a, b, c]

Expected output:

ans = [10 0 23]

Slicing keeps sparse storage

S = sparse([1; 3; 2], [1; 1; 3], [10; 30; 23], 3, 3);
T = S([1 2], [1 3]);
issparse(T)

Expected output:

ans = 1

Sparse arithmetic with dense and scalar operands

S = sparse([1; 3; 2], [1; 1; 2], [10; 30; 20], 3, 2);
A = full(3 .* S);
B = S + 2;

Expected output:

`A` is sparse-scaled then densified by `full`; `B` is a full matrix because sparse zeros become twos.

Using sparse with coding agents

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

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

FAQ

Which sparse construction forms are supported?⌄

RunMat supports sparse(A), sparse(m,n), sparse(i,j,v), sparse(i,j,v,m,n), and sparse(i,j,v,m,n,nzmax). MATLAB-compatible value storage is double, single, or logical; exact integer value storage is available only in runmat compatibility mode.

Does RunMat store sparse matrices densely?⌄

No. The runtime uses compressed sparse column storage with column pointers, row indices, and stored values.

What happens to duplicate triplets?⌄

Duplicate row/column pairs are summed, matching MATLAB sparse assembly semantics.

Can sparse matrices live on the GPU?⌄

Not yet. If you pass a gpuArray to sparse, RunMat gathers it and builds a host sparse matrix. Native GPU sparse handles can be added once the acceleration API grows sparse storage.

Which operations interoperate with sparse matrices today?⌄

Core introspection such as size, numel, class, whos, nnz, find, scalar and slice indexing, real and complex +, -, and .* interop, transpose, and conjugate transpose understand sparse values. Broader sparse linear algebra will build on this representation.

Can sparse matrices be assigned through indexing?⌄

Not yet. RunMat currently raises RunMat:SparseAssignmentUnsupported for sparse indexed assignment before validating slice selectors, so unsupported writes fail deterministically.

Related Array functions

Creation

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

Grouping

accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply

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 · 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 sparse is executed, line by line, in Rust.

  • View the source for sparse 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.

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
  • How sparse works
  • Does RunMat run sparse on the GPU?
  • GPU memory and residency
  • Examples
  • Creating a sparse matrix from triplets
  • Summing duplicate row and column pairs
  • Converting a full matrix
  • Creating an empty sparse matrix
  • Reading stored and unstored entries
  • Slicing keeps sparse storage
  • Sparse arithmetic with dense and scalar operands
  • Using sparse with coding agents
  • FAQ
  • Related Array functions
  • Creation
  • Grouping
  • Sorting Sets
  • Shape
  • Indexing
  • Introspection
  • Open-source implementation
  • About RunMat