Map function in MATLAB?

I am a little surprised that MATLAB does not have a Map function, so I hacked it myself, since I can not live without it. Is there a better version? Is there any standard functional programming library for MATLAB where I am absent?

function results = map(f,list) % why doesn't MATLAB have a Map function? results = zeros(1,length(list)); for k = 1:length(list) results(1,k) = f(list(k)); end end 

will be for example

 map( @(x)x^2,1:10) 
+84
map-function functional-programming matlab
Jun 11 '09 at 19:34
source share
7 answers

Short answer: the built-in ARRAYFUN function does exactly what your map function does for numeric arrays:

 >> y = arrayfun(@(x) x^2,1:10) y = 1 4 9 16 25 36 49 64 81 100 

There are two other built-in functions that behave similarly: CELLFUN (which works with elements of cell arrays) and STRUCTFUN (which works with each field of the structure).

However, these functions are often not needed if you take advantage of vectorization, in particular using arithmetic operators . For the example you specified, a vectorized solution would be:

 >> x = 1:10; >> y = x.^2 y = 1 4 9 16 25 36 49 64 81 100 

Some operations will automatically work through elements (for example, adding a scalar value to a vector), while other operators have special syntax for an elementary operation (indicated by the β€œ.” Symbol before the operator). Many functions in MATLAB are designed to work with vector and matrix arguments using elementary operations and, therefore, do not require map functions.

To summarize, here are a few different ways to square each element in an array:

 x = 1:10; %// Sample array f = @(x) x.^2; %// Anonymous function that squares each element of its input %// Option #1: y = x.^2; %// Use the element-wise power operator %// Option #2: y = f(x); %// Pass a vector to f %// Option #3: y = arrayfun(f,x); %// Pass each element to f separately 

Of course, for such a simple operation, option No. 1 is the most reasonable choice.

+121
Jun 11 '09 at 19:48
source share

In addition to vector and elementary operations, there is also cellfun for displaying functions over arrays of cells. For example:

 cellfun(@upper, {'a', 'b', 'c'}, 'UniformOutput',false) ans = 'A' 'B' 'C' 

If "UniformOutput" is true (or not provided), it will try to combine the results according to the size of the cell array, therefore

 cellfun(@upper, {'a', 'b', 'c'}) ans = ABC 
+10
Jun 11 '09 at 20:17
source share

A fairly simple solution using Matlab vectorization:

 a = [ 10 20 30 40 50 ]; % the array with the original values b = [ 10 8 6 4 2 ]; % the mapping array c = zeros( 1, 10 ); % your target array 

Now typing

 c( b ) = a 

returns

 c = 0 50 0 40 0 30 0 20 0 10 

c (b) is a reference to a vector of size 5 with elements c at the indices given by b. Now, if you are Assing the values ​​of this reference vector, the original values ​​in c are overwritten, since c (b) contains references to the values ​​in c and no copies.

+3
Nov 17 2018-11-11T00:
source share

If Matlab does not have a built-in map function, this may be due to efficiency considerations. In your implementation, you use a loop to iterate over the elements of a list, which is generally not approved in the matlab world. Most matlab built-in functions are "vectorized", i.e. E. It is more efficient to call a function over the entire array than to iterate over it yourself and call a function for each element.

In other words, this

 a = 1:10; a.^2 

much faster than this

 a = 1:10; map(@(x)x^2, a) 

assuming your definition of a card.

+1
Jun 11 '09 at 19:48
source share

It seems that the built-in arrayfun does not work if the necessary array is needed: for example: mapping (@ (x) [xx ^ 2 x ^ 3], 1:10)

the small modifications below make this work better:

 function results = map(f,list) % why doesn't MATLAB have a Map function? for k = 1:length(list) if (k==1) r1=f(list(k)); results = zeros(length(r1),length(list)); results(:,k)=r1; else results(:,k) = f(list(k)); end; end; end 
+1
Apr 26 2018-12-12T00:
source share

You do not need map , since the scalar function applied to the list of values ​​is applied to each of the values ​​and, therefore, works similarly to map . Just try

 l = 1:10 f = @(x) x + 1 f(l) 

In your particular case, you can even write

 l.^2 
0
Jun 11 '09 at 19:54
source share

Vectorizing a solution, as described in previous answers, is probably the best solution for speed. Vectorization is also very matrabi and feels good.

With that said, Matlab now has a Map container class.

See http://www.mathworks.com/help/matlab/map-containers.html

-one
May 02 '14 at 21:34
source share



All Articles