Mercurial > hg > octave-nkf
comparison scripts/general/arrayfun.m @ 6439:a37c427ae9d1
[project @ 2007-03-23 15:04:01 by jwe]
author | jwe |
---|---|
date | Fri, 23 Mar 2007 15:05:01 +0000 |
parents | 9b982dd07654 |
children | 81a73cdd87c0 |
comparison
equal
deleted
inserted
replaced
6438:14e5882b6269 | 6439:a37c427ae9d1 |
---|---|
1 ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> | 1 ## Copyright (C) 2006 Bill Denney <denney@seas.upenn.edu> |
2 ## | 2 ## |
3 ## This program is free software; you can redistribute it and/or modify | 3 ## This file is part of Octave. |
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 ## | 4 ## |
8 ## This program is distributed in the hope that it will be useful, | 5 ## Octave is free software; you can redistribute it and/or modify it |
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | 6 ## under the terms of the GNU General Public License as published by |
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 7 ## the Free Software Foundation; either version 2, or (at your option) |
11 ## GNU General Public License for more details. | 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. | |
12 ## | 14 ## |
13 ## You should have received a copy of the GNU General Public License | 15 ## You should have received a copy of the GNU General Public License |
14 ## along with this program; if not, write to the Free Software | 16 ## along with Octave; see the file COPYING. If not, write to the Free |
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 ## 02110-1301, USA. | |
16 | 19 |
17 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
18 ## @deftypefn {Command} @var{a} = arrayfun(@var{name}, @var{c}) | 21 ## @deftypefn {Command} {@var{a} =} arrayfun (@var{name}, @var{c}) |
19 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}) | 22 ## @deftypefnx {Command} {@var{a} =} arrayfun (@var{func}, @var{c}) |
20 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{d}) | 23 ## @deftypefnx {Command} {@var{a} =} arrayfun (@var{func}, @var{c}, @var{d}) |
21 ## @deftypefnx {Command} @var{a} = arrayfun(@var{func}, @var{c}, @var{options}) | 24 ## @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{}) | 25 ## @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 | 26 ## 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 | 27 ## functions that do not accept array arguments. If the function does |
25 ## accept array arguments it is better to call the function directly. | 28 ## accept array arguments it is better to call the function directly. |
26 ## | 29 ## |
27 ## See @code{cellfun} for complete usage instructions. | 30 ## See @code{cellfun} for complete usage instructions. |
28 ## @seealso{cellfun} | 31 ## @seealso{cellfun} |
29 ## @end deftypefn | 32 ## @end deftypefn |
30 | 33 |
31 function [varargout] = arrayfun(func, varargin) | 34 function varargout = arrayfun (func, varargin) |
32 | 35 |
33 if (nargin < 2) | 36 if (nargin < 2) |
34 print_usage(); | 37 print_usage (); |
35 endif | 38 endif |
36 | 39 |
37 ## convert everything to cells and call cellfun (let cellfun error | 40 ## Convert everything to cells and call cellfun (let cellfun error |
38 ## check the options in case more options come available) | 41 ## check the options in case more options come available). |
39 sizetomatch = size(varargin{1}); | 42 sizetomatch = size (varargin{1}); |
40 m2cargs{1} = ones (size (varargin{1}, 1), 1); | 43 m2cargs{1} = ones (size (varargin{1}, 1), 1); |
41 m2cargs{2} = ones (size (varargin{1}, 2), 1); | 44 m2cargs{2} = ones (size (varargin{1}, 2), 1); |
42 cfarg{1} = mat2cell (varargin{1}, m2cargs{:}); | 45 cfarg{1} = mat2cell (varargin{1}, m2cargs{:}); |
43 stillmatches = true; | 46 stillmatches = true; |
44 idx = 1; | 47 idx = 1; |
45 while stillmatches && (idx < length(varargin)) | 48 while (stillmatches && idx < numel (varargin)) |
46 idx++; | 49 idx++; |
47 thissize = size (varargin{idx}); | 50 thissize = size (varargin{idx}); |
48 if (length (thissize) == length (sizetomatch)) && \ | 51 if (numel (thissize) == numel (sizetomatch) |
49 all (thissize == sizetomatch) | 52 && all (thissize == sizetomatch)) |
50 if ischar (varargin{idx}) && \ | 53 if (ischar (varargin{idx}) |
51 (strcmpi (varargin{idx}, "UniformOutput") || \ | 54 && (strcmpi (varargin{idx}, "UniformOutput") |
52 strcmpi (varargin{idx}, "ErrorHandler")) | 55 || strcmpi (varargin{idx}, "ErrorHandler"))) |
53 ## catch these strings just in case they happen to be the same | 56 ## Catch these strings just in case they happen to be the same |
54 ## size as the other input. | 57 ## size as the other input. |
55 stillmatches = false; | 58 stillmatches = false; |
56 else | 59 else |
57 cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:}); | 60 cfarg{idx} = mat2cell (varargin{idx}, m2cargs{:}); |
58 endif | 61 endif |
60 stillmatches = false; | 63 stillmatches = false; |
61 endif | 64 endif |
62 endwhile | 65 endwhile |
63 | 66 |
64 varargout = cell (max ([nargout, 1]), 1); | 67 varargout = cell (max ([nargout, 1]), 1); |
65 [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:length(varargin)}); | 68 [varargout{:}] = cellfun (func, cfarg{:}, varargin{idx+1:numel(varargin)}); |
66 endfunction | 69 endfunction |
67 | 70 |
68 %!test | 71 %!test |
69 %! fun = @(x,y) 2*x+y | 72 %! fun = @(x,y) 2*x+y; |
70 %! A = [1,2;3,4]; | 73 %! A = [1,2;3,4]; |
71 %! assert(arrayfun(fun,A,A),fun(A,A)) | 74 %! assert(arrayfun(fun,A,A),fun(A,A)) |