]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/map.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / map.m
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>
5 ##
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
9 ## version.
10 ##
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
14 ## details.
15 ##
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/>.
18
19 ## -*- texinfo -*-
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.
22 ##
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).
31 ##
32 ## Example:
33 ## @example
34 ##
35 ## octave> A
36 ## A
37 ## @{
38 ##   [1,1] = 0.0096243
39 ##   [2,1] = 0.82781
40 ##   [1,2] = 0.052571
41 ##   [2,2] = 0.84645
42 ## @}
43 ## octave> B
44 ## B =
45 ## @{
46 ##   [1,1] = 0.75563
47 ##   [2,1] = 0.84858
48 ##   [1,2] = 0.16765
49 ##   [2,2] = 0.85477
50 ## @}
51 ## octave> map(@@min,A,B)
52 ## ans =
53 ## @{
54 ##   [1,1] = 0.0096243
55 ##   [2,1] = 0.82781
56 ##   [1,2] = 0.052571
57 ##   [2,2] = 0.84645
58 ## @}
59 ## @end example
60 ## @seealso{reduce, match}
61 ## @end deftypefn
62
63 function return_type = map (fun_handle, data_struct, varargin)
64
65   persistent warned = false;
66   if (! warned)
67     warned = true;
68     warning ("Octave:deprecated-function",
69              "map has been deprecated, and will be removed in the future. Use `arrayfun' or `cellfun' instead.");
70   endif
71
72   if (nargin < 2)
73     print_usage;
74   elseif (!(isnumeric (data_struct) || iscell (data_struct)))
75     error ("second argument must be either a matrix or a cell object");
76   endif
77
78   if (isa (fun_handle, "function_handle"))
79     ##do nothing
80   elseif (ischar (fun_handle))
81     fun_handle = str2func (fun_handle);
82   else
83     error ("fun_handle must either be a function handle or the name of a function");
84   endif
85
86   if (iscell (data_struct))
87     return_type = cellfun (fun_handle, data_struct,varargin{:});
88   else
89     return_type = arrayfun (fun_handle, data_struct,varargin{:});
90   endif
91
92 endfunction
93
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))