spones

Replaces the nonzero elements of a matrix by ones.

Category: Mathematics > Sparse Matrices

Syntax

R = spones(S)

Returns a sparse matrix with the same pattern as S and ones where S is nonzero.

Description

spones keeps the structure of a matrix and discards the values, giving its adjacency pattern. For a sparse input it equals spfun(@(x) 1, S). It is handy for counting entries by row or column, as in sum(spones(S), 2).

Input arguments

S
Sparse or full matrix.

Output arguments

R
Sparse matrix of ones and zeros.

Example

Pattern and row counts

S = sparse([2 0 3; 0 0 -5]);
full(spones(S))
full(sum(spones(S), 2))
Output
ans = 2×3

     1     0     1
     0     0     1
ans = 2×1

     2
     1

See also

spfun sparse nnz spy