6422
|
1 ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> |
|
2 ## |
6439
|
3 ## This file is part of Octave. |
6422
|
4 ## |
6439
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6439
|
9 ## |
|
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. |
6422
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6422
|
18 |
|
19 ## -*- texinfo -*- |
6714
|
20 ## @deftypefn {Function File} {@var{a} =} arrayfun (@var{name}, @var{c}) |
|
21 ## @deftypefnx {Function File} {@var{a} =} arrayfun (@var{func}, @var{c}) |
|
22 ## @deftypefnx {Function File} {@var{a} =} arrayfun (@var{func}, @var{c}, @var{d}) |
|
23 ## @deftypefnx {Function File} {@var{a} =} arrayfun (@var{func}, @var{c}, @var{options}) |
|
24 ## @deftypefnx {Function File} {[@var{a}, @var{b}, @dots{}] =} arrayfun (@var{func}, @var{c}, @dots{}) |
6439
|
25 ## Execute a function on each element of an array. This is useful for |
|
26 ## functions that do not accept array arguments. If the function does |
6422
|
27 ## accept array arguments it is better to call the function directly. |
|
28 ## |
|
29 ## See @code{cellfun} for complete usage instructions. |
|
30 ## @seealso{cellfun} |
|
31 ## @end deftypefn |
|
32 |
6439
|
33 function varargout = arrayfun (func, varargin) |
6422
|
34 |
|
35 if (nargin < 2) |
6439
|
36 print_usage (); |
6422
|
37 endif |
|
38 |
6439
|
39 ## Convert everything to cells and call cellfun (let cellfun error |
|
40 ## check the options in case more options come available). |
|
41 sizetomatch = size (varargin{1}); |
6422
|
42 m2cargs{1} = ones (size (varargin{1}, 1), 1); |
|
43 m2cargs{2} = ones (size (varargin{1}, 2), 1); |
|
44 cfarg{1} = mat2cell (varargin{1}, m2cargs{:}); |
|
45 stillmatches = true; |
|
46 idx = 1; |
6439
|
47 while (stillmatches && idx < numel (varargin)) |
6422
|
48 idx++; |
|
49 thissize = size (varargin{idx}); |
6439
|
50 if (numel (thissize) == numel (sizetomatch) |
|
51 && all (thissize == sizetomatch)) |
|
52 if (ischar (varargin{idx}) |
|
53 && (strcmpi (varargin{idx}, "UniformOutput") |
|
54 || strcmpi (varargin{idx}, "ErrorHandler"))) |
|
55 ## Catch these strings just in case they happen to be the same |
6422
|
56 ## size as the other input. |
|
57 stillmatches = false; |
|
58 else |
|
59 cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:}); |
|
60 endif |
|
61 else |
|
62 stillmatches = false; |
|
63 endif |
|
64 endwhile |
|
65 |
|
66 varargout = cell (max ([nargout, 1]), 1); |
6439
|
67 [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:numel(varargin)}); |
6422
|
68 endfunction |
|
69 |
|
70 %!test |
6439
|
71 %! fun = @(x,y) 2*x+y; |
6422
|
72 %! A = [1,2;3,4]; |
|
73 %! assert(arrayfun(fun,A,A),fun(A,A)) |