1 ## Copyright (C) 2003 Tomer Altman <taltman@lbl.gov>
2 ## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@mavs.uta.edu>
3 ## Copyright (C) 2012 Carnë Draug <carandraug+dev@gmail.com>
4 ## Copyright (C) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
6 ## This program is free software; you can redistribute it and/or modify it under
7 ## the terms of the GNU General Public License as published by the Free Software
8 ## Foundation; either version 3 of the License, or (at your option) any later
11 ## This program is distributed in the hope that it will be useful, but WITHOUT
12 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 ## You should have received a copy of the GNU General Public License along with
17 ## this program; if not, see <http://www.gnu.org/licenses/>.
20 ## @deftypefn{Function File} {@var{result} =} map (@var{function}, @var{iterable}, @dots{})
21 ## Apply @var{function} to every item of @var{iterable} and return the results.
23 ## @code{map}, like Lisp's ( & numerous other language's ) function for
24 ## iterating the result of a function applied to each of the data
25 ## structure's elements in turn. The results are stored in the
26 ## corresponding input's place. For now, just will work with cells and
27 ## matrices, but support for structs are intended for future versions.
28 ## Also, only "prefix" functions ( like @code{min (a, b, c, ...)} ) are
29 ## supported. FUN_HANDLE can either be a function name string or a
30 ## function handle (recommended).
51 ## octave> map(@@min,A,B)
60 ## @seealso{reduce, match}
63 function return_type = map (fun_handle, data_struct, varargin)
65 persistent warned = false;
68 warning ("Octave:deprecated-function",
69 "map has been deprecated, and will be removed in the future. Use `arrayfun' or `cellfun' instead.");
74 elseif (!(isnumeric (data_struct) || iscell (data_struct)))
75 error ("second argument must be either a matrix or a cell object");
78 if (isa (fun_handle, "function_handle"))
80 elseif (ischar (fun_handle))
81 fun_handle = str2func (fun_handle);
83 error ("fun_handle must either be a function handle or the name of a function");
86 if (iscell (data_struct))
87 return_type = cellfun (fun_handle, data_struct,varargin{:});
89 return_type = arrayfun (fun_handle, data_struct,varargin{:});
94 %!assert(map(@min,[1 2 3 4 5],[5 4 3 2 1]), [1 2 3 2 1])
95 %!assert(map(@min,rand(1,5),[0 0 0 0 0]), [0 0 0 0 0])
96 %!assert(map(@(x,y) (sin(x).^2 + cos(y).^2),-pi:0.5:+pi,-pi:0.5:+pi),ones(1,13))