Skip to content

nonzeros

Column vector of non-zero elements.

v = nonzeros(A)

Returns a column vector of the non-zero elements of A, in column-major order. Works on both sparse and full matrices. Length of the result equals nnz(A).

S = sparse([1 0 0; 0 2 0; 3 0 0]);
nonzeros(S) % [1; 3; 2] (column-major order)
  • nnz — Number of non-zero elements.
  • sparse — Create a sparse matrix.