MATLAB Function Reference | Search  Help Desk |
spfun | Examples |
Apply function to nonzero sparse matrix elements
f = spfun('Thefunction
',
S)
spfun
function selectively applies a function to only the nonzero elements of a sparse matrix, preserving the sparsity pattern of the original matrix (except for underflow).
f = spfun('function
',S)
evaluates function
(S)
on the nonzero elements of S
. function
must be the name of a function, usually defined in an M-file, which can accept a matrix argument, S
, and evaluate the function at each element of S
.
Functions that operate element-by-element, like those in the elfun
directory, are the most appropriate functions to use with spfun
.
Given the 4-by-4 sparse diagonal matrix
S = (1,1) 1 (2,2) 2 (3,3) 3 (4,4) 4
f
=
spfun('exp',S)
has the same sparsity pattern as S
:
f = (1,1) 2.7183 (2,2) 7.3891 (3,3) 20.0855 (4,4) 54.5982whereas
exp(S)
has 1
s where S
has 0
s.
full(exp(S)) ans = 2.7183 1.0000 1.0000 1.0000 1.0000 7.3891 1.0000 1.0000 1.0000 1.0000 20.0855 1.0000 1.0000 1.0000 1.0000 54.5982