MATLAB - Map Function



The map function in MATLAB is a powerful tool that allows you to apply a specific operation or function to each element in an array or cell array, returning the results in a new array or cell array. This is particularly useful when you want to perform the same operation on multiple data points without having to write a loop.

Matlab does not come with a built-in function map, however, you can achieve similar functionality using other built-in functions and constructs.

Using arrayfun for Arrays

The arrayfun function in MATLAB allows you to apply a function to each element of an array, similar to the concept of mapping.

Syntax

B = arrayfun(func, A)

The detailed explanation of the syntax is as follows −

B − This is the output array, which is the result of applying the function func to each element of the input array A.

func − This is the function you want to apply to each element of the input array. It can be an anonymous function or a function handle. func should accept one input argument (the current element of A) and return a single value.

A − This is the input array to which you want to apply the function func.

Example 1

Let's say you have an array of numbers and each of the numbers in the array has to be squared.So for this we can make use of arrayfun as shown in the example below.

% This is a function to square a number
square_function = @(x) x^2;

% Array of numbers to be squared 
input_array = [1, 2, 3, 4, 5];

% Use arrayfun to apply the square function to each element
output_array = arrayfun(square_function, input_array);

% Display the result
disp(output_array)

In this example, square_function is applied to each element in the input_array, and the results are stored in the output_array. The result is [1, 4, 9, 16, 25].

arrayfun simplifies the process of applying a function to each element of an array and helps us avoid doing the same thing using loops.

When you execute the same in matlab command window the output is −

>> % This is a function to square a number
square_function = @(x) x^2;

% Array of numbers to be squared 
input_array = [1, 2, 3, 4, 5];

% Use arrayfun to apply the square function to each element
output_array = arrayfun(square_function, input_array);

% Display the result
disp(output_array)

     1     4     9    16    25

Using cellfun for Cell Arrays

For cell arrays, you can use the cellfun function to apply a function to each element of the cell array.

Syntax

B = cellfun(func, C)

B − This is the output cell array, which is the result of applying the function func to each element of the input cell array C.

func − This is the function you want to apply to each element of the cell array. func can be an anonymous function or a function handle. The function should accept one input argument (the current element of C) and return a single value.

C − This is the input cell array to which you want to apply the function func.

Example 1: Applying a Function to a Cell Array of Strings

Consider you have a cell array of strings,and you want to know the length of each string present in the cell array. For that you can make use of cellfun() as shown in the example below −

% Create a cell array of strings
input_cell_array = {'apple', 'banana', 'cherry'};

% Define a function to find the length of a string
length_function = @(str) length(str);

% Use cellfun to apply the length function to each element
output_array = cellfun(length_function, input_cell_array);

% Display the result
disp(output_array)

In this example, length_function is applied to each string in the input_cell_array, and the results are stored in the output_array.

When you execute above code in matlab command window the output is as follows −

>> % Create a cell array of strings
input_cell_array = {'apple', 'banana', 'cherry'};

% Define a function to find the length of a string
length_function = @(str) length(str);

% Use cellfun to apply the length function to each element
output_array = cellfun(length_function, input_cell_array);

% Display the result
disp(output_array)

     5     6     6

Example 2: Applying a Function to a Cell Array of Numbers

Let's say you have a cell array of numeric arrays, and you want to find the sum of each numeric array. For this lets make use of cellfun() to be used on each of the cell array as shown below −

% Create a cell array of numeric arrays
numeric_cells = { [1, 2, 3], [4, 5], [6, 7, 8, 9] };

% Define a function to compute the sum of an array
sum_function = @(arr) sum(arr);

% Use cellfun to apply the sum function to each element
output_array = cellfun(sum_function, numeric_cells);

% Display the result
disp(output_array)

In this example, sum_function is applied to each numeric array in the numeric_cells cell array, and the results are stored in the output_array. Let us check the output of the above code by executing in the matlab command window.

>> % Create a cell array of numeric arrays
numeric_cells = { [1, 2, 3], [4, 5], [6, 7, 8, 9] };

% Define a function to compute the sum of an array
sum_function = @(arr) sum(arr);

% Use cellfun to apply the sum function to each element
output_array = cellfun(sum_function, numeric_cells);

% Display the result
disp(output_array)

     6     9    30
Advertisements