R = spfun(fun, S)
| 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. |
| 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. |
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.
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)
| Version | Description |
|---|---|
| 2.0.0 | initial version |