MATLAB mapping table data structure (CONTAINERS.MAP) _matlab

Source: Internet
Author: User
Tags map class map data structure strcmp unique id

I have not seen the MATLAB has a class dict structure, this can not but say is a kind of regret. Of course, this superficial understanding, also let me earlier and Julia knot the fate, gain it. From 2010 to 2013, I was mainly using MATLAB, such as DataSet (=>table) and other data structures (Matrix, cell,struct). Recently saw a lot of people in writing related articles, specially turn, for Matlab feel happy. In fact, the dict structure of MATLAB is also some, is Hou Wei matlab should be corrected.
--Preface.

For map, you can see the official document. Http://cn.mathworks.com/help/matlab/ref/containers.map-class.html?requestedDomain=www.mathworks.com

MATLAB mapping table data structure

2016-4-25 15:59| Published by: ilovematlab| View: 2954| Rating: 2

Absrtact: In addition to the commonly used basic data types, MATLAB has many other useful data types are not familiar with, such as mapping table containers. Map, commonly used in MATLAB advanced data types. Its greatest feature is to use convenient indexing method for fast lookup. This article describes why the need for this data structure to ...

Directory:
• about the author
containers. Map Introduction
• The limitations of arrays, cellular and structural bodies
• What is containers. Map
containers. Properties and member methods of map
containers. The characteristics of Map
containers. Map can be continuously expanded without the need for pre-allocation
containers. Map can be directly modified as a parameter within a function
containers. Map enhances readability of programs
containers. Map provides a quick lookup of data
containers. Use instances of Map
• Used to hold the periodic table of elements
• Used to achieve fast retrieval
MATLAB Common basic data types are: integral type, floating-point type, character type, function handle, cell array and structure body array. In addition to these basic data types, MATLAB has many other data types are not familiar with, these data types are also very useful in programming. MATLAB Advanced data Type series is designed to introduce them to you: such as containers. Map,tables,enumeration and time series and so on, why they are useful, what problems to solve, and how to use in scientific engineering calculations. This article first introduces containers. The MAP data type.
Containers. Map Introduction
The most representative of the advanced data type in MATLAB is Containers.map, we can call it the mapping table. It is similar to a function map, for example, the definition of function mapping is: F (x) = Y

For each x, there is a unique y corresponding to it, which is not necessarily the opposite. As shown in Figure Fig.1. Similar to function mappings, the mapping table is used to describe the one by one correspondence between the key and the key value (key value). Each key is unique and can only be used for one key Value. As shown in Figure fig.2.

Fig.1 function Mapping Relationship
fig.2 containers. Mapping diagram of Map class
Limitations of arrays, cellular and structural bodies
Let's start by introducing the limitations of arrays, cell arrays, and structs, and why sometimes these basic data types don't meet the requirements of the program, in other words, why we need containers. The MAP data structure. Suppose you want to use MATLAB to record the data in the phone book, such as the table table.1:
Table.1 Phone Book

Name

Phone number

Abby 5086470001
Bob 5086470002
Charlie 5086470003
The array is discussed first, because the phone book contains both numbers and strings, and the array can only hold data of type double, so there is no way to directly record the contents of the telephone number in an array. Try the cell array again, we declare a 3 X 3 cell array in line 1th, and then fill in the order of the number book in line 2-4. % Cell Array Initialization
AddressBook = cell (3,1); % pre-allocation size is a good habit of MATLAB programming
Addressbook{1} = {' Abby ', ' 5086470001 '};
Addressbook{2} = {' Bob ', ' 5086470002 '};
5.addressbook{3} = {' Charlie ', ' 5086470003 '};

When needed, the contents can be accessed through a for loop, such as: for iter = 1:length (addressbook)% cell array traversal
Addressbook{iter}% accessing the contents of the cell via index
End

But in order to traverse the phone book is not practical use, the main function of the number book should be to provide the search function is. For example, to check Charlie's phone number, we hope that the program can best be written as follows: Charlienum = addressbook{' Charlie '}% Hint: This is the wrong way to write

or charlienum = addressbook.charlie% Hint: This is the wrong wording

However, the value of the cell array can only access the content through index, and does not support the access method as above. So in order to find Charlie's phone number, the program had to traverse all the contents of the cell, take out the string of the first column of each cell element and compare it, if the name equals Charlie, the output phone number:% use for loop to find
For iter = 1:length (addressbook)
If strcmp (Addressbook{iter}{1}, ' Charlie ')
ADDRESSBOOK{ITER}{2}% if found then output phone number
5. Break;
End
End

Of course, there are other ways to fill in the phone book, such as the phone and name stored in two cell array to names = {' Abby ', ' Bob ', ' Charlie '}; % with cell number
Numbers = {' 5086470001 ', ' 5086470002 ', ' 5086470001 '}; % place names in cell
IND = Find (strcmp (names, ' Charlie ')); % strcmp Accept vector input returns logical array
% find finds the index of the 0 element in the logical array immediately
5.numbers{ind}

The 3rd line strcmp accepts the cell as input, in which the search for the Charlie,find function returns the location of Charlie, which is faster than using a for loop, but without exception, both methods have to traverse an array from the beginning, which is slow after all. In addition to finding slow performance, there are other drawbacks to using cell-phone book type data: • It is not easy to verify duplicate data. The phone book requires each person's name to be unique, so in the data entry to prevent the duplication of names, but we have no other way to know whether a name has been used, unless each input in the entire cell to do traversal comparison.
• It is not easy to add content. If the records in the phone book need to grow constantly, but we do not have the means to estimate the approximate number of the first, so the memory can not be effectively allocated, so when you add data, once more than the predetermined amount, the MATLAB will be reassigned memory.
• Unable to easily delete content. If we want to remove a record from the cell, we can find the record and place the cell contents of the location empty, but this does not automatically reduce the length of the cell array, if the deletion is much, the cell will leave a lot of vacant space.
• Inconvenient as a function of the parameters, the specific reasons see struct limitations.
Finally, let's try this. Use the structure to hold the phone book data type, struct assignment is very simple, such as can be directly assigned:% Assignment Method 1
Addressbook.abby = ' 5086470001 ';
Addressbook.bob = ' 5086470002 ';
Addressbook.charlie = ' 5086470003 ';

Or:% Assignment Method 2
AddressBook = struct (' Abby ', ' 5086470001 ', ' Bob ', ' 5086470002 ', ' Charlie ', ' 5086470003 ')

Method 1 and Method 2 are equivalent. struct data type lookup is convenient, for example, to query Charlie's phone number, direct access to the field of the same name in struct. num = Addressbook.charlie

If the name to be queried is a variable, we can use the GetField function: num = GetField (addressbook,name)% where name is a variable

struct in the phone book type of data, query up really more than the cellular progress, but still some inconvenient place. Because the name of the field of struct must start with a letter, which is a big limitation, not all similar phone book structure can be indexed by people names, such as account number book, stock code and so on, they usually start with a number, such as the data in Figure table.2:
TABLE.2 stock Code of Deep proof

Stock code

Stock Name

000001 Deep Development
20,000 section
000004 St Country Agriculture
If we ask to find the specific information of stock through the stock code, struct can't simply do it directly. Another inconvenient thing about using struct is that when you use struct as a function parameter and you need to make some modifications to the struct within the function, in order to return the modified result, we need to make a full copy of the original struct. Obviously, this is inefficient if there is a lot of data in the struct. For example, in the following function, AddressBook is passed as a parameter of a function, a new field is added to the function, and in order to return the updated structure, a new struct must be reconstructed inside the function, which is the caller of the function that s returns to. % struct as a function parameter
function S = modifystruct (s)
S.dave = ' 5086470004 ';
End

with containers. Map to record phone book
In the previous section we introduced the limitations of arrays, cell arrays, and structs in simulating the data structure of the phone book, and we'll see how to use Containers in this section. Map to hold the contents of the phone book: Addressmap = containers. Map; % first declares a mapping Table object variable
Addressmap (' Abby ') = ' 5086470001 ';
Addressmap (' Bob ') = ' 5086470002 ';
Addressmap (' Charlie ') = ' 5086470003 ';

First line we declare a containers. The variables of the map (in fact, containers). The object of the map class is called the addressmap,2,3,4 line assigns a value to an object by providing the key key value, where the value inside the single quotation mark is called the key, and the right side of the equation is called the key value. Through this structure, we build up the mapping relation data structure of graph fig.3 in MATLAB memory.

fig.3 Phone Book Mapping table
The UML lookup of the Fig.4 map class is extremely handy for finding the contents of Addressmap objects, such as finding the phone number of Charlie we want to know, just:% find
num = Addressmap (' Charlie ')

If we want to change Charlie's phone number, just:% assignment
Addressmap (' Charlie ') = Newnum;

What is containers. Map
Containers. Map is a built-in MATLAB class (class is a basic concept of object-oriented programming, where you can temporarily understand the class as a data type), the so-called built-in, is the internal implementation of MATLAB, usually this kind of performance is more excellent. Containers. Map where containers is the name of the package, the map is a class in the package, and the map is its class name. This class is represented by the syntax of UML (Unified Modeling Language), and its properties include count, Keytype,valuetype. Its common methods include keys,values,iskey,remove. As shown in Figure fig.4.
Containers. Properties and member methods of map
In this section we introduce containers. The properties and member methods of the map, assuming that we initialize a containers. Map object:% initialize a Map object
Addressmap = containers. Map;
Addressmap (' Abby ') = ' 5086470001 ';
Addressmap (' Bob ') = ' 5086470002 ';
5.addressMap (' Charlie ') = ' 5086470003 ';

Type the name of the object on the command line, and MATLAB displays the basic information about the object's properties: >> addressmap
Addressmap =
Map with properties:
Number of mapping pairs in count:3% map
5. Type of key in Keytype:char% MAP
Type of key value in valuetype:any% MAP

Where count represents the number of mapping pairs in the Map object. According to the rules, the type of the key must be a character type, not other data types, and the type of the key value can be any type of matlab: array, cell, structure, MATLAB objects, even Java objects and so on. Because the type of the key value can be any MATLAB type, so containers. Map is a very flexible data type in MATLAB. The Member method keys are used to return all the keys in the object: >> Addressmap.keys
Ans =
' Charlie ' Abby ' Bob '

Member method values are used to return all the key values in an object >> addressmap.values
Ans =
' 5086470003 ' 5086470001 ' 5086470002 '

Remove is used to remove a key-key-value pair from an object, after which the value of count in the object becomes 2. >> addressmap.remove (' Charlie ')
Ans =
Map with properties:
Count:2% maps to fewer numbers
5. Keytype:char
Valuetype:any

The IsKey member method is used to determine whether a map object already contains a key value, such as: >> Addressmap.iskey (' Abby ')
Ans =
1

The Addressmap.iskey (' Abby ')% key is case sensitive
5.ans =
0

IsKey's function is to query, similar to the previously mentioned find and STRCMP functions together use examples.
Containers. The characteristics of Map

Containers. Map can be continuously expanded without the need for pre-allocation
Using arrays and cell arrays as data containers, you typically have to allocate the container's size in advance. Otherwise each expansion of the container, MATLAB will reallocate memory, and copy the original array of data to the newly allocated memory. The mapping table is a container that can be expanded flexibly and does not need to be allocated, each time adding content to the inside does not cause the MATLAB to reallocate memory. We can construct the mapping table object by providing two cells, for example, we construct a ticket book data structure: The initialization method of the% mapping table 1
Ticketmap = containers. Map ({' 2r175 ', ' B7398 ', ' a479gy '}, ...)
{' Abby ', ' Bob, ' Charlie '});

You can also declare an object at the beginning of the program: The initialization method of the% mapping table 2

Ticketmap = containers. Map

And then slowly inserting data into the table during the calculation: the initialization method of the% mapping table 2

ticketmap[' 2r175 '] = ' Abby ';
...
ticketmap[' a479gy '] = ' Charlie;

Containers. Map can be directly modified as a parameter within a function
Because containers. Map is the handle class (handle class of introduction see "Matlab object-oriented Programming-from the introduction to the design Pattern" Chapter III: MATLAB, the handle class and real value class), we can also conveniently pass this object to any MATLAB function, And inside the function directly modify the data inside the object, and do not use it as a return value output, such as: >> Modifymap (TICKETMAP);

Modifymap function can be written as: function Modifymap (ticketmap)% The function has no return value
.....
Ticketmap (NewKey) = NewID
End

Note that the modified Ticketmap is not used as the return value, in the function we directly modify the data in the map, this is the handle class in the MATLAB object-oriented language characteristics.
Containers. Map enhances readability of programs
Within the mapping table, you can store various types of data, and give them some meaningful names. Concrete examples are shown in the periodic table of elements. When accessing and modifying this data, you can use these meaningful names directly to increase the readability of the program. If you use a cell array to store this data, when accessing and modifying the data, you need to use the index of the integer, the program readability is not good enough.
Containers. Map provides a quick lookup of data
The complexity of the mapping table lookup is constant O (C), while the complexity of the traditional array and cell array lookup is linear O (N). If you are unfamiliar with the concept of complexity in the algorithm, you can understand this: if you use an array or a cell to store the data, when we want to search for a value in the data structure, we can only do a linear search, on average, the time of the search and the number of elements in the data structure n is proportional to, that is, the more elements in the cell and array, The longer it takes to find a value, this is called the linear Complexity O (N). The underlying implementation of the map is a hash table (hash map), the search time is constant C, in theory, the search speed and the number of elements in the collection is not related. So when the number of elements in the container is large, to find Fast, you can use containers. Map structure. See examples of quick lookups for specific examples. The following is a comparison of arrays and container by looking up a hypothetical set of data. The performance of the map. Our first line constructs an array of 1000000 (10^7) integers (this is the number of elements in the array, if there are only a few elements, the efficiency of the linear lookup is higher, and the integer from 1 to (10^7) is populated sequentially; As a comparison, the second row constructs a containers. A map object that fills an integer from 1 to (10^7) internally, with the same key and KeyValue. A = 1:1 million;
m = containers. Map (1:1000000,ones (1,1000000));

Array data structure, using the Find function to look up from 1 to (10^7) of integers, on the author's machine, takes 28.331901 seconds

Find in% array
Tic
For i=1:100000,
Find (B==i);
End
Toc

% Command line results
Elapsed is 28.331901 seconds.

Containers. The map data structure, using the key value 1 to (10^7) for access, on the author's machine, takes only 1.323007 seconds, the conclusion is: if there is a large number of data, and frequent search operations, you can consider the use of containers. The map data structure. (Readers can try to reduce the number of elements in the container and see what happens when the array looks faster.) )

Lookup of% Map table
Tic
For i=1:100000,
M (i);
End
Toc

% Command line results

Elapsed is 1.323007 seconds.

When it comes to using advanced data structures in MATLAB, another common practice is to use Java and Python objects directly, and here, by the way, compare the efficiency of a hash table using Java in MATLAB. On the writer's machine, it takes 3.072889 seconds.

% Java Hash Table lookup
s = Java.util.HashSet ();
For i=1:100000, S.add (i); End
Tic
5.for i=1:100000,
S.contains (i);
End
Toc

% command line results 5.

Elapsed is 3.072889 seconds.

Containers. Use instances of Map

Used to keep the periodic table of elements
There are many examples of such key-key values that can be used in engineering calculations. For example, the physical chemistry calculation can use the mapping table to hold the contents of the periodic table: >> ptable = containers. Map;

PTable (' H ') = 1;
PTable (' he ') = 2;

Where the key is an atomic symbol, and the key value is the atomic weight. Because the type of the key value is not limited, the key value itself can also be objects, such as the Atom class object, which contains more useful content:% class file ATOM.M
ClassDef Atom < handle
Properties
Atomicnumber
5. FullName
End
Methods
function obj = Atom (num,name)
Obj.atomicnumber = num;
Obj.fullname = Name
End
End
End

\end{verbatim} So: the% key value can be an Atom object

PTable (' H ') = Atom (1, ' hydrogen ');
PTable (' H ') = Atom (2, ' helium ');

To enable fast retrieval
This question comes from Ilovematlab a netizen's question:
Hello everyone, recently encountered a problem, to build more than 20,000 cubic vector, and to ensure that each time not repeated construction, the vector element value in the 2000―3000 between the number, the current approach is to establish a historical file matrix A, build a storage line, a size row number continues to increase. Each time a new vector is constructed, the vector is compared to each row of the historical file matrix to determine whether it was built or not, and rebuilt. The algorithm shows that the check operation time is more than 20,000 times, more than 200S of running time. Want to strive to reduce the time, to find ways. Thanks a lot.
The problem with this user is not how to build these vectors, but how to ensure that they are not duplicated every time. The solution he came up with was to use matrix A to store historical files, to create a new vector, to compare it to what was already in the history file, and to build it if there was no archive. The problem with this design is that, as he said, when a is stored in a quantity that is more than enough, the time to check whether the vector has been created before is lengthened. This is a typical problem that can be solved with a mapping table, which turns linear complexity into a constant complexity. He can design a separate ID for each vector, such as direct digital to id:num2str (vector)% build
MyDictionary = containers. Map

% Insert Data
5.id = num2str (vector); % such as Vector = [1 2 3];
MyDictionary (' some_id ') = vector;

The subsequent search also obtains a unique ID by vector and then iskey to determine if the key value has been added to the map. Some_otherid = Some_other_vector;
% validation has been built to
If Mydictinary.iskey (' Some_otherid ')
% do the work to be done
5.end

About author
Oopmatlab, Ph. D. In computational physics, Master of Computer Science, MATLAB object-oriented programming-from Introduction to design mode, is now working in a scientific engineering computing company as the Architecture Group C + + software engineer. Amateur interests include how to apply modern tools in software engineering to science and engineering calculations to better solve complex problems. It includes how to design the structure of the Scientific Computing program, how to make the program easy to expand and modify, how to ensure that the function of the program has not been affected when the algorithm is improved and developed, and how to make the algorithm development and the test system develop hand in hand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.