sparse — Create sparse double matrices from full arrays, sizes, or row/column/value triplets.
sparse creates a sparse double matrix. RunMat stores sparse matrices in compressed sparse column form and supports MATLAB construction forms for conversion from full arrays, empty sparse allocation by size, and triplet assembly from row indices, column indices, and values.
Syntax
S = sparse(A)
S = sparse(m, n)
S = sparse(i, j, v)
S = sparse(i, j, v, m, n)
S = sparse(i, j, v, m, n, nzmax)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Full or sparse matrix to convert. |
m | SizeArg | Yes | — | Number of rows. |
n | SizeArg | Yes | — | Number of columns. |
i | NumericArray | Yes | — | One-based row subscripts. |
j | NumericArray | Yes | — | One-based column subscripts. |
v | NumericArray | Yes | — | Values for each row/column pair. |
nzmax | SizeArg | No | — | Allocation hint accepted for MATLAB compatibility. |
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 sparse works
sparse(A)converts a full numeric or logical array to a sparse double matrix, storing only nonzero values.sparse(m, n)returns anm x nsparse 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 anm x nsparse 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, matching MATLAB.
- Sparse matrices support scalar row/column indexing and linear indexing; scalar selections return sparse
1 x 1values, 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:SparseAssignmentUnsupportedinstead 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 = 3Summing duplicate row and column pairs
S = sparse([1; 1; 2], [2; 2; 3], [4; 5; 6], 2, 3);
[r, c, v] = find(S);
vExpected 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 = 0Reading 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 = 1Sparse 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) for real double/logical data.
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 · eye · false · fill · 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
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.