1 ## Copyright (C) 2004-2012 David Bateman and Andy Adler
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{y} =} spfun (@var{f}, @var{S})
21 ## Compute @code{f(@var{S})} for the non-zero values of @var{S}.
22 ## This results in a sparse matrix with the same structure as
23 ## @var{S}. The function @var{f} can be passed as a string, a
24 ## function handle, or an inline function.
25 ## @seealso{arrayfun, cellfun, structfun}
28 function y = spfun (f, S)
37 if (isa (f, "function_handle") || isa (f, "inline function"))
38 y = sparse (i, j, f(v), m, n);
40 y = sparse(i, j, feval (f, v), m, n);
45 %!assert(spfun('exp',[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0]))
46 %!assert(spfun('exp',sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0]))
47 %!assert(spfun(@exp,[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0]))
48 %!assert(spfun(@exp,sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0]))