MATLAB Function Reference |
Apply function to nonzero sparse matrix elements
Syntax
Description
The spfun
function selectively applies a function to only the nonzero elements of a sparse matrix S
, preserving the sparsity pattern of the original matrix (except for underflow or if fun
returns zero for some nonzero elements of S
).
f = spfun(fun,S)
evaluates fun(S)
on the nonzero elements of S
. fun
is a function handle. See Function Handles in the MATLAB Programming documentation for more information.
Parameterizing Functions Called by Function Functions, in the MATLAB mathematics documentation, explains how to provide additional parameters to the function fun
, if necessary.
Remarks
Functions that operate element-by-element, like those in the elfun
directory, are the most appropriate functions to use with spfun
.
Examples
Given the 4-by-4 sparse diagonal matrix
Because fun
returns nonzero values for all nonzero element of S
, f
=
spfun(@exp,S)
has the same sparsity pattern as S.
whereas 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
See Also
function_handle
(@
)
speye | sph2cart |
© 1994-2005 The MathWorks, Inc.