spfun
Apply a function to the nonzero elements of a sparse matrix.
📝Syntax
R = spfun(fun, S)
📥Input Arguments
Parameter Description
fun a function handle applied to the vector of nonzero values.
S a sparse matrix. A full matrix is first converted to sparse.
📤Output Arguments
Parameter Description
R a sparse matrix with the same size and nonzero locations as S, whose values are fun applied to the nonzero values of S.
📄Description

spfun evaluates fun only on the nonzero elements of S, which avoids applying the function to the many stored zeros and preserves the sparse structure.

The function handle must accept and return a column vector of the same length. Any resulting zero value is pruned from the sparse result.

💡Examples
S = sparse([2 0 -3; 0 4 0]);
R = spfun(@(x) x .* 10, S)
S = sparse([2 0; 0 4]);
R = spfun(@(x) 1 ./ x, S)
🔗See Also
sponesnonzerosfind
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub