NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/fmap.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function array = fmap(fun, array, varargin)
2 %FMAP   Evaluate a function for each element of an array.
3 %
4 %   ARRAY2 = FMAP(FUN, ARRAY1) evaluates the function FUN on all
5 %   elements of ARRAY.  FUN must be a string and ARRAY1 must be numeric
6 %   array, a character array or a cell array.
7 %
8 %   ARRAY2 = FMAP(FUN, ARRAY1, X1, X2, ...) passes extra arguments to
9 %   FUN.
10 %
11 %   Examples:
12 %
13 %     fmap('sqrt', [ 4 9 ])               returns  [ 2 3 ]
14 %
15 %     fmap('sqrt', { 4 9 })               returns  { 2 3 }
16 %
17 %     fmap('power', { 4 9 }, 3)           returns  { 64 729 }
18 %
19 %     fmap('all', { [ 1 1 0 ] [ 1 1 ] })  returns  { 0 1 }
20
21 %   Author:      Peter J. Acklam
22 %   Time-stamp:  2000-02-29 23:58:28
23 %   E-mail:      jacklam@math.uio.no
24 %   WWW URL:     http://www.math.uio.no/~jacklam
25
26    error(nargchk(2, Inf, nargin));
27
28    if isnumeric(array) | ischar(array)
29
30       eval(sprintf([ ...
31          'for i = 1 : prod(size(array))\n' ...
32          '   array(i) = %s(array(i), varargin{:});\n' ...
33          'end\n' ], fun));
34
35    elseif isa(array, 'cell')
36
37       eval(sprintf([ ...
38          'for i = 1 : prod(size(array))\n' ...
39          '   array{i} = %s(array{i}, varargin{:});\n' ...
40          'end\n' ], fun));
41
42    else
43
44       error([ mfilename ' does not support the class ' ...
45                '"' class(array) '".' ]);
46
47    end
Note: See TracBrowser for help on using the browser.