Write a Map Function - MATLAB & Simulink - MathWorks América Latina (2024)

Write a Map Function

Role of Map Function in MapReduce

mapreduce requires both an input map function that receives blocks of data and that outputs intermediate results, and an input reduce function that reads the intermediate results and produces a final result. Thus, it is normal to break up a calculation into two related pieces for the map and reduce functions to fulfill separately. For example, to find the maximum value in a data set, the map function can find the maximum value in each block of input data, and then the reduce function can find the single maximum value among all of the intermediate maxima.

This figure shows the Map phase of the mapreduce algorithm.

Write a Map Function- MATLAB & Simulink- MathWorks América Latina (1)

The Map phase of the mapreduce algorithmhas the following steps:

  1. mapreduce reads a single block of data using the read function on the input datastore, then calls the map function to work on the block.

  2. The map function then works on the individual block of data and adds one or more key-value pairs to the intermediate KeyValueStore object using the add or addmulti functions.

  3. mapreduce repeats this process for each of the blocks of data in the input datastore, so that the total number of calls to the map function is equal to the number of blocks of data. The ReadSize property of the datastore determines the number of data blocks.

The Map phase of the mapreduce algorithm is complete when the map function processes each of the blocks of data in the input datastore. The result of this phase of the mapreduce algorithm is a KeyValueStore object that contains all of the key-value pairs added by the map function. After the Map phase, mapreduce prepares for the Reduce phase by grouping all the values in the KeyValueStore object by unique key.

Requirements for Map Function

mapreduce automatically calls the map function for each block of data in the input datastore. The map function must meet certain basic requirements to run properly during these automatic calls. These requirements collectively ensure the proper movement of data through the Map phase of the mapreduce algorithm.

The inputs to the map function are data, info,and intermKVStore:

In addition to these basic requirements for the map function,the key-value pairs added by the map function must also meet theseconditions:

  1. Keys must be numeric scalars, character vectors, or strings. Numeric keys cannot be NaN, complex, logical, or sparse.

  2. All keys added by the map function must have the sameclass.

  3. Values can be any MATLAB® object, including allvalid MATLAB data types.

Note

The above key-value pair requirements may differ when usingother products with mapreduce. See the documentationfor the appropriate product to get product-specific key-value pairrequirements.

Sample Map Functions

Here are a few illustrative map functions used in mapreduce examples.

Identity Map Function

A map function that simply returns what mapreduce passes to it is called an identity mapper. An identity mapper is useful to take advantage of the grouping of values by unique key before doing calculations in the reduce function. The identityMapper mapper file is one of the mappers used in the example Tall Skinny QR (TSQR) Matrix Factorization Using MapReduce.

function identityMapper(data, info, intermKVStore) % This mapper function simply copies the data and add them to the % intermKVStore as intermediate values. x = data.Value{:,:}; add(intermKVStore,'Identity', x);end

Simple Map Function

One of the simplest examples of a nonidentity mapper is maxArrivalDelayMapper, which is the mapper for the example Find Maximum Value with MapReduce. For each chunk of input data, this mapper calculates the maximum arrival delay and adds a key-value pair to the intermediate KeyValueStore.

function maxArrivalDelayMapper (data, info, intermKVStore) partMax = max(data.ArrDelay); add(intermKVStore, 'PartialMaxArrivalDelay',partMax);end

Advanced Map Function

A more advanced example of a mapper is statsByGroupMapper, which is the mapper for the example Compute Summary Statistics by Group Using MapReduce. This mapper uses a nested function to calculate several statistical quantities (count, mean, variance, and so on) for each chunk of input data, and then adds several key-value pairs to the intermediate KeyValueStore object. Also, this mapper uses four input arguments, whereas mapreduce only accepts a map function with three input arguments. To get around this, pass in the extra parameter using an anonymous function during the call to mapreduce, as outlined in the example.

function statsByGroupMapper(data, ~, intermKVStore, groupVarName) % Data is a n-by-3 table. Remove missing values first delays = data.ArrDelay; groups = data.(groupVarName); notNaN =~isnan(delays); groups = groups(notNaN); delays = delays(notNaN); % Find the unique group levels in this chunk [intermKeys,~,idx] = unique(groups, 'stable'); % Group delays by idx and apply @grpstatsfun function to each group intermVals = accumarray(idx,delays,size(intermKeys),@grpstatsfun); addmulti(intermKVStore,intermKeys,intermVals); function out = grpstatsfun(x) n = length(x); % count m = sum(x)/n; % mean v = sum((x-m).^2)/n; % variance s = sum((x-m).^3)/n; % skewness without normalization k = sum((x-m).^4)/n; % kurtosis without normalization out = {[n, m, v, s, k]}; endend

See Also

mapreduce | tabularTextDatastore | add | addmulti | KeyValueStore

Related Topics

  • Write a Reduce Function
  • Getting Started with MapReduce

Comando de MATLAB

Ha hecho clic en un enlace que corresponde a este comando de MATLAB:

 

Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.

Write a Map Function- MATLAB & Simulink- MathWorks América Latina (2)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Write a Map Function
- MATLAB & Simulink
- MathWorks América Latina (2024)

FAQs

How to create a map in Matlab? ›

Set up a new map by using the newmap function. By default, map axes objects use an Equal Earth projection centered on the prime meridian and the equator. Display the land areas using the geoplot function. You can use map axes to create maps in any supported projected coordinate reference system (CRS).

What is the map function in Matlab? ›

The map function then works on the individual block of data and adds one or more key-value pairs to the intermediate KeyValueStore object using the add or addmulti functions.

How to map a signal in Simulink? ›

After you import signals or buses, you can map data.
  1. On the Root Inport Mapper toolbar, to check if a data set is mappable, click Check Map Readiness. You can then select another map mode and check the readiness again. ...
  2. In the left pane, click a data set to see the mapping results.

How to write a MATLAB function? ›

Syntax for Function Definition
  1. function myOutput = myFunction(x) If your function returns more than one output, enclose the output names in square brackets.
  2. function [one,two,three] = myFunction(x) If there is no output, you can omit it.
  3. function myFunction(x) Or you can use empty square brackets.

What is the function of a map? ›

A map is a symbolic representation of selected characteristics of a place, usually drawn on a flat surface. Maps present information about the world in a simple, visual way. They teach about the world by showing sizes and shapes of countries, locations of features, and distances between places.

What is the map () method used for? ›

map() method. .map() creates an array from calling a specific function on each item in the parent array. .map() is a non-mutating method that creates a new js array, as opposed to mutating methods, which only make changes to the calling array. This method can have many uses when working with arrays.

What does a function map look like? ›

On a mapping diagram, a function looks like each input only has one arrow starting from it; if there is more than one arrow from an input value to different y-values, then it is not a function. "This is an example of a function because each input value is mapped to only one output value."

What is a Simulink signal in MATLAB? ›

You can create a Simulink. Signal object in the MATLAB® workspace or in a model workspace. Use signal objects to assign or validate signal or discrete state attributes by giving the signal or discrete state the same name as the workspace variable that references the Simulink. Signal object.

What is Simulink signals? ›

In Simulink®, signals are the outputs of dynamic systems represented by blocks in a Simulink diagram and by the diagram itself. The lines in a block diagram represent mathematical relationships among the signals defined by the block diagram.

How to see data in Simulink? ›

Visualize Simulation Data in Simulink
  1. Scope block: View connected signals in a separate compact window.
  2. Dashboard block library: Build an interactive interface to control and monitor the simulation by using graphical controls and displays.
  3. Record block: View and log signals to the workspace or a file.
Jan 5, 2023

How to create an equation in Simulink? ›

To insert an equation interactively:
  1. Go to the Insert tab and click Equation. A blank equation appears.
  2. Build your equation by selecting symbols, structures, and matrices from the options displayed in the Equation tab. ...
  3. Format your equation using the options available in the Text section.

How to generate s function in Simulink? ›

To add the S-Function Builder block to a model, click the Simulink canvas and type S-Function Builder or drag a S-Function Builder block from Simulink Library > User-Defined. To open the S-Function Builder editor, double-click the S-Function Builder block icon or select the block.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Roderick King

Last Updated:

Views: 5447

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.