5164
|
1 ## Copyright (C) 2004 David Bateman & Andy Adler |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
5307
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
5164
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {@var{y} =} spfun (@var{f},@var{x}) |
|
20 ## Compute @code{f(@var{x})} for the non-zero values of @var{x}. |
|
21 ## This results in a sparse matrix with the same structure as |
|
22 ## @var{x}. The function @var{f} can be passed as a string, a |
|
23 ## function handle or an inline function. |
|
24 ## @end deftypefn |
|
25 |
6498
|
26 function t = spfun (f, s) |
|
27 |
5164
|
28 if (nargin != 2) |
6046
|
29 print_usage (); |
5164
|
30 endif |
|
31 |
6498
|
32 if (issparse (s)) |
|
33 [i,j,v,m,n] = spfind (s); |
5164
|
34 else |
6498
|
35 [i, j, v] = find (s); |
|
36 [m, n] = size (s); |
5164
|
37 end |
|
38 |
6220
|
39 if (isa (f, "function_handle") || isa (f, "inline function")) |
6498
|
40 t = sparse (i, j, f(v), m, n); |
5164
|
41 else |
6498
|
42 t = sparse(i, j, feval (f, v), m, n); |
5164
|
43 endif |
|
44 |
|
45 endfunction |
|
46 |
|
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])) |
|
49 %!assert(spfun(@exp,[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0])) |
|
50 %!assert(spfun(@exp,sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0])) |
|
51 |