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

NameTypeRequiredDefaultDescription
ANumericArrayYesFull or sparse matrix.

Returns

NameTypeDescription
vNumericArrayFull column vector.

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 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.
  • Logical input returns double ones for true elements.
  • Complex input returns a complex column vector and omits only elements whose real and imaginary parts are both zero.

GPU memory and residency

nonzeros gathers dense GPU tensors before scanning. A future provider path can preserve residency for dense GPU reductions and compaction.

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.

Creation

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

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

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.