
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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