1
## Copyright (C) 2003 Tomer Altman
2
## Copyright (C) 2007 Muthiah Annamalai
4
## This program is free software; you can redistribute it and/or
5
## modify it under the terms of the GNU General Public
6
## License as published by the Free Software Foundation;
7
## either version 2, or (at your option) any later version.
9
## This software is distributed in the hope that it will be useful,
10
## but WITHOUT ANY WARRANTY; without even the implied
11
## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
## PURPOSE. See the GNU General Public License for more
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
15
## You should have received a copy of the GNU General Public
16
## License along with this software; see the file COPYING. If not,
17
## see <http://www.gnu.org/licenses/>.
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/>.
21
## @deftypefn{Function File} {@var{result} = } map ( fun_handle, varargin )
24
## usage: result = map ( FUN_HANDLE, ARG1, ... )
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.
26
23
## @code{map}, like Lisp's ( & numerous other language's ) function for
27
24
## iterating the result of a function applied to each of the data
63
## @seealso{reduce, match, apply}
60
## @seealso{reduce, match}
66
## Author: Tomer Altman
67
## Keywords: map matrix cell
68
## Maintainer: Tomer Altman
69
## Created: November 15, 2003
72
63
function return_type = map (fun_handle, data_struct, varargin)
75
if (ischar (fun_handle))
76
fun_handle = eval (strcat ("@", fun_handle));
78
fstr = typeinfo (fun_handle);
80
error ("Error: Cannot find function handle, or funtion doesnt exist");
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.");
85
error ("map: incorrect number of arguments; expecting at least two");
86
elseif (strcmp (fstr, "function handle") == 0)
87
error ("map: first argument is not a valid function handle");
88
74
elseif (!(isnumeric (data_struct) || iscell (data_struct)))
89
error ("map: second argument must be either a matrix or a cell object");
92
[rows, cols] = size (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");
95
86
if (iscell (data_struct))
97
return_type = cell (rows, cols);
100
return_type = zeros (rows, cols);
103
otherdata = length (varargin);
104
val = cell (1, otherdata+1);
111
val {1} = data_struct {i, j};
112
for idx = 2:otherdata+1
113
val {idx} = varargin {idx-1}{i,j};
115
return_type {i,j} = apply (fun_handle, val);
121
return_type {i,j} = fun_handle (data_struct {i,j});
129
val {1} = data_struct (i,j);
130
for idx = 2:otherdata+1
131
val {idx} = varargin {idx-1}(i,j);
133
return_type (i, j) = apply (fun_handle, val);
139
return_type (i, j) = fun_handle (data_struct (i, j));
87
return_type = cellfun (fun_handle, data_struct,varargin{:});
89
return_type = arrayfun (fun_handle, data_struct,varargin{:});
148
%! assert(map(@min,[1 2 3 4 5],[5 4 3 2 1]), [1 2 3 2 1])
149
%! assert(map(@min,rand(1,5),[0 0 0 0 0]), [0 0 0 0 0])
150
%! assert(map(@(x,y) (sin(x).^2 + cos(y).^2),-pi:0.5:+pi,-pi:0.5:+pi),ones(1,13))
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))