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