Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Software Engineering Tutorial
  • Software Development Life Cycle
  • Waterfall Model
  • Software Requirements
  • Software Measurement and Metrics
  • Software Design Process
  • System configuration management
  • Software Maintenance
  • Software Development Tutorial
  • Software Testing Tutorial
  • Product Management Tutorial
  • Project Management Tutorial
  • Agile Methodology
  • Selenium Basics
Open In App
Next Article:
map() Function in R
Next article icon

Map Function in MATLAB

Last Updated : 22 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

A map function basically takes the elements of the array and applies a function to each element.  The resultant output is of the same shape as the array, but the values are the result of the function. In MATLAB, there is a similar function called arrayfun(), which we can be used to achieve the same task. The output from this arrayfunc can have any data type.

Syntax:

X = arrayfun(func,A)

Parameters:

  • fun: It is a function that we are going to map on each element of the array A.
  • A: It is the array on which we  are going to map the function.
  • X: In variable X the output is going to be stored from the map function.

Here, arrayfun(funct,A) applies the function funct to the elements of array A. arrayfun then concatenates the outputs from funct into the output array X, so that for the ith element of A, X(i) = funct(A(i)). 

The input argument funct is a function handle to a function that takes one input argument and returns a scalar. 

Let's take an example for a better understanding:

Example 1:

Matlab
% MAP function in MatLab  % creating a dummy array  % on which we are going to map a function A = [1,2,3,4,5,6,7,8,9,10];  % Applying arrayfun()   output = arrayfun(@(x) x*2, A);  % displaying output fprintf("output is :"); disp(output); 

Output:

Applying arrayfun()

In the above function, firstly we have created an array to which we are going to map a custom function. The custom is x*2 i.e. multiply each element in the array by 2.

 Then we are using arrayfun(@(x) x*2, A), lets break down this statement here arrayfun(fun,array) is the general syntax for mapping a function to the array. We are using @(x) and x*2 in the statement where x*2 is the custom function that we are applying and @(x) is acts as each element of that array A. After that, we're storing the output to a variable and then using fprintf function we printed a string on display and using the disp function we display the output.

Example 2:

Matlab
% MAP function in MatLab  % creating two dummy arrays  % on which we are going to map a function A = [1,2,3,4,5]; B = [6,7,8,9,10];  % Applying arrayfun()   output = arrayfun(@(x,y) x*y, A,B);  % displaying output fprintf("Product is :"); disp(output); 

Output:

Product to two arrays using arrayfun()

Next Article
map() Function in R

A

amnindersingh1414
Improve
Article Tags :
  • Software Engineering
  • Blogathon-2021
  • MATLAB-Functions

Similar Reads

  • Local Functions in MATLAB
    Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a function:Before moving forward let's see how actually t
    2 min read
  • map() Function in R
    In R Programming Language the Map function is a very useful function used for element-wise operations across vectors or lists. This article will help show how to use it with multiple code examples. Map Function in RThe Map function in R belongs to the family of apply functions, designed to make oper
    3 min read
  • Find() function in MATLAB
    The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
    3 min read
  • Filter Function in MATLAB
    The filter function or 1-D digital filter is a function in MATLAB that is used to filter a given noisy data by removing the noise in the data and sharpening or smoothing the input function. As MATLAB provides a dedicated Signal Processing Toolset, the filter function comes handy to remove noise from
    3 min read
  • Nested Functions in MATLAB
    Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big.  Declaring a Function: To declare a function in MATLAB we use given
    3 min read
  • Parse Function Inputs in MATLAB
    Basically, the parse function is a method that users use in the function to convert data from one format to another. In programming, parsing is the processing and analysis of strings of numbers. We can also say that the parse function is often used to convert strings to other data types such as numb
    4 min read
  • Private Functions in MATLAB
    Private functions are useful when you want to limit the scope of a function. Here we will learn how to create private functions and also use them. Private functions are primary functions that are visible only to a limited group of other functions. Generally, we make private functions, if we want to
    2 min read
  • Anonymous Functions in MATLAB
    A block of code that is organized in such a way that is reusable for the entire program. Functions are used for reducing efforts made by writing code and making the program short, and easily understandable.  There are different syntaxes for declaring a function in different programming languages. In
    5 min read
  • Mapping Functions in LISP
    In this article, we will discuss mapping functions in lisp. Mapping functions are applied on the list data structure for combining one or more lists of elements. By using this we can perform mathematical operations and can join the elements. The main advantage of this function is that we can combine
    2 min read
  • Scripts and Functions in MATLAB
    In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences