Mercurial > hg > octave-lyh
comparison scripts/general/arrayfun.m @ 6422:9b982dd07654
[project @ 2007-03-20 21:26:12 by dbateman]
author | dbateman |
---|---|
date | Tue, 20 Mar 2007 21:26:13 +0000 |
parents | |
children | a37c427ae9d1 |
comparison
equal
deleted
inserted
replaced
6421:cac156381f81 | 6422:9b982dd07654 |
---|---|
1 ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> | |
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 | |
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Command} @var{a} = arrayfun(@var{name}, @var{c}) | |
19 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}) | |
20 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{d}) | |
21 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{options}) | |
22 ## @deftypefnx {Command} [@var{a}, @var{b}, @dots{}] = arrayfun(@var{func}, @var{c}, @dots{}) | |
23 ## Execute a function on each element of an array. This is useful for | |
24 ## functions that do not accept array arguments. If the function does | |
25 ## accept array arguments it is better to call the function directly. | |
26 ## | |
27 ## See @code{cellfun} for complete usage instructions. | |
28 ## @seealso{cellfun} | |
29 ## @end deftypefn | |
30 | |
31 function [varargout] = arrayfun(func, varargin) | |
32 | |
33 if (nargin < 2) | |
34 print_usage(); | |
35 endif | |
36 | |
37 ## convert everything to cells and call cellfun (let cellfun error | |
38 ## check the options in case more options come available) | |
39 sizetomatch = size(varargin{1}); | |
40 m2cargs{1} = ones (size (varargin{1}, 1), 1); | |
41 m2cargs{2} = ones (size (varargin{1}, 2), 1); | |
42 cfarg{1} = mat2cell (varargin{1}, m2cargs{:}); | |
43 stillmatches = true; | |
44 idx = 1; | |
45 while stillmatches && (idx < length(varargin)) | |
46 idx++; | |
47 thissize = size (varargin{idx}); | |
48 if (length (thissize) == length (sizetomatch)) && \ | |
49 all (thissize == sizetomatch) | |
50 if ischar (varargin{idx}) && \ | |
51 (strcmpi (varargin{idx}, "UniformOutput") || \ | |
52 strcmpi (varargin{idx}, "ErrorHandler")) | |
53 ## catch these strings just in case they happen to be the same | |
54 ## size as the other input. | |
55 stillmatches = false; | |
56 else | |
57 cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:}); | |
58 endif | |
59 else | |
60 stillmatches = false; | |
61 endif | |
62 endwhile | |
63 | |
64 varargout = cell (max ([nargout, 1]), 1); | |
65 [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:length(varargin)}); | |
66 endfunction | |
67 | |
68 %!test | |
69 %! fun = @(x,y) 2*x+y | |
70 %! A = [1,2;3,4]; | |
71 %! assert(arrayfun(fun,A,A),fun(A,A)) |