nonzeros — Return nonzero elements in column-major order.
nonzeros(A) returns a full column vector containing the nonzero elements of A in column-major order.
Syntax
v = nonzeros(A)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Full or sparse matrix. |
Returns
| Name | Type | Description |
|---|---|---|
v | NumericArray | Full column vector. |
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 nonzeros works
- Sparse input returns the stored sparse values in CSC column order.
- Dense numeric input is scanned in column-major order and zeros are omitted.
- All eight integer classes preserve their exact class and values, including wide
int64anduint64elements aboveflintmax. - Logical input returns a logical column of true elements and character input returns a character column excluding only null characters.
- Complex input returns a same-component-class complex column and omits only elements whose real and imaginary parts are both zero.
- Supported explicit resident real, floating-complex, logical, or integer input returns an explicit resident result through the exact owner; automatic residency may gather transparently and return host data if restoration is unavailable. Typed complex-integer device storage remains unsupported.
GPU memory and residency
nonzeros currently compacts on the host after an owner-aware non-destructive download. Supported explicit real, floating-complex, logical, and integer gpuArray intent is preserved by uploading the same-class full column result to the exact source owner; failure to preserve explicit residency is an error rather than silent host success. Automatic residency may gather transparently, while typed complex-integer device storage remains unsupported.
Example
Read stored sparse values
S = sparse([1; 3; 2], [1; 1; 2], [10; 30; 20], 3, 2);
nonzeros(S)Expected output:
ans = [10; 30; 20]Using nonzeros with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how nonzeros changes the result.
Run a small nonzeros example, explain the result, then change one input and compare the output.
FAQ
Is the output sparse?⌄
No. MATLAB returns a full column vector, and RunMat does the same.
Related Array functions
Creation
colon · createArray · empty · eye · false · full · inf · linspace · logspace · magic · meshgrid · nan · nchoosek · ndgrid · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · 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 nonzeros is executed, line by line, in Rust.
- View the source for nonzeros 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.