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.

What is the world map function in Matlab? ›

worldmap( latlim , lonlim ) allows you to define a custom geographic region in terms of its latitude and longitude limits in degrees. worldmap( Z , R ) derives the map limits from the extent of a regular data grid, Z , georeferenced by R . h = worldmap(___) returns the handle of the axesm -based map.

How to plot data on a map in Matlab? ›

Create Geographic Line Plot

Specify the latitude and longitude for each city, then plot the data using the geoplot function. Customize the appearance of the line using the line specification '-*' . Adjust the latitude and longitude limits of the map using geolimits . Change the basemap using the geobasemap function.

How to make a map step by step? ›

How to Make a Map
  1. Choose a map template. Choose a map that fits your purpose. ...
  2. Label important locations and areas. Use text and graphics (such as push pins, arrows, and other symbols) to label the map with key information. ...
  3. Add a compass. ...
  4. Include a legend.

What is a map in functions? ›

In many branches of mathematics, the term map is used to mean a function, sometimes with a specific property of particular importance to that branch. For instance, a "map" is a "continuous function" in topology, a "linear transformation" in linear algebra, etc.

How do you read a function map? ›

We can read it as ' f ' is a function from A to B. If “ f “ is a function from A to B and x ∈ A and y ∈ B, then we can say that y is the image of element x under the function ' f ' and denote it by f (x). Here, element x is called the preimage of y.

How do you write a global function in MATLAB? ›

Create a function in your current working folder that returns the value of a global variable. These two functions have separate function workspaces, but they both can access the global variable. function r = getGlobalx global x r = x; Set the value of the global variable, x , and obtain it from a different workspace.

How to plot a graph in MATLAB Simulink? ›

From the Simulink Editor, right-click the logging badge for a signal, and select Properties to open the Instrumentation Properties for the signal. Then, click the graphic representation of the Line. From the Instrumentation Properties, you can also select subplots where you want to plot the signal.

How to load a map in MATLAB? ›

MATLAB's base maps for global and regional scales are loaded from the web by default, but the data can also be downloaded from MATLAB Add-Ons to be used without an internet connection. Web maps with street-level data, topography, or other specialized base layer representations can be loaded from third-party sources.

How to plot a map? ›

Add a place
  1. On your computer, sign in to My Maps.
  2. Open or create a map. A map can have up to 10,000 lines, shapes, or places.
  3. Click Add marker .
  4. Select a layer and click where to put the place. A layer can have 2,000 lines, shapes, or places.
  5. Give your place a name.
  6. Click Save.

How do you create an array map? ›

The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.

How to plot 3d map in MATLAB? ›

plot3( X , Y , Z ) plots coordinates in 3-D space.
  1. To plot a set of coordinates connected by line segments, specify X , Y , and Z as vectors of the same length.
  2. To plot multiple sets of coordinates on the same set of axes, specify at least one of X , Y , or Z as a matrix and the others as vectors.

References

Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5781

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.