spones — Replace nonzero sparse matrix entries with ones.
spones(S) returns a sparse matrix with the same nonzero pattern as S and stored values set to one.
Syntax
R = spones(S)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Full or sparse matrix. |
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 spones works
- Sparse input preserves row indices and column pointers while replacing each stored value with
1. - Dense numeric and logical input are first converted to sparse pattern storage.
- NaN values count as stored values, matching RunMat sparse storage semantics.
- Complex sparse storage is not available in RunMat's current
SparseTensorrepresentation.
GPU memory and residency
spones gathers dense GPU tensors and returns a host sparse matrix because RunMat sparse matrices are currently host-resident.
Example
Preserve a sparse pattern
S = sparse([1; 3], [2; 2], [4; 9], 3, 3);
full(spones(S))Expected output:
ans = [0 1 0; 0 0 0; 0 1 0]Using spones with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how spones changes the result.
Run a small spones example, explain the result, then change one input and compare the output.
FAQ
Does spones preserve explicit sparse zeros?⌄
RunMat's sparse constructor does not store final zero values, so spones operates on the stored nonzero pattern.
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 · 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 spones is executed, line by line, in Rust.
- View the source for spones 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.