Use a numeric array as Map value
Show older comments
I'm reading through a file and want to create a map of string keys, each with an array of numeric arrays. How do I go about declaring this object and then appending arrays to the values?
1 Comment
Answers (1)
If I understand well what you are trying to achieve, you can:
- Build your own system using a cell array of keys and STRCMP/STRCMPI/REGEXP for matching key/value pairs.
- Use a containers.Map object.
- Use a Java Hashtable object ( java.util.Hashtable )
Note that if you need to store complex data structures, a good option is to use a hybrid approach, where you define a values cell array, a keys hashtable, and then store complex data structures in the cell array and only their IDs in the hashtable.
5 Comments
Ben
on 19 Jul 2015
You want more than "an array of arrays". The first thing that you have to understand is the difference between numeric arrays and cell arrays.
Numeric arrays must be rectangular, homogeneous in type/class (i.e. all elements are of class double), etc; this is quite restrictive but allows efficient numerical computing. For example:
>> a = [1, 2, 3; 4, 5, 6]
a =
1 2 3
4 5 6
>> class( a )
ans =
double
>> size( a )
ans =
2 3
>> a * 8
ans =
8 16 24
32 40 48
Numeric arrays are block-indexed using parentheses:
>> a(2,1)
ans =
4
>> a(2,2:3)
ans =
5 6
On the other hand, cell arrays (= arrays of cells) are versatile containers; they don't allow computing, but there is no limitation on what cells can contain. For example:
>> c = {'Ben', 56, [1, 2, 3; 4, 5, 6], {'Age', 53}}
c =
'Ben' [56] [2x3 double] {1x2 cell}
>> class( c )
ans =
cell
As you can see, they are defined using curly brackets, and their content can be quite inhomogeneous. They can be block-indexed using parentheses, which return a block of cell(s), but not their content:
>> c(3:4)
ans =
[2x3 double] {1x2 cell}
>> class( c(3:4) )
ans =
cell
>> c(1)
ans =
'Ben'
>> class( c(1) )
ans =
cell
A cell is a container; it is not its content. To address the content of a cell, we use curly bracket indexing; {} means "content of". To get the content of cell 1, for example:
>> c{1}
ans =
Ben
>> class( c{1} )
ans =
char
Now the content of cells can be indexed further, using its specific indexing type. If it is a numeric array:
>> c{3}
ans =
1 2 3
4 5 6
>> class( c{3} )
ans =
double
>> c{3}(1:2,2:3)
ans =
2 3
5 6
If it is a cell array again, this cell array can be {}-indexed again:
>> c{4}
ans =
'Age' [53]
>> class( c{4} )
ans =
cell
>> c{4}{1}
ans =
Age
>> class( c{4}{1} )
ans =
char
>> c{4}{1}(1:2)
ans =
Ag
This is standard usage of numeric and cell arrays. But you want more, because you want to be able to index the content of whatever data structure you will use using a string as an index. For that, you have roughly the three options that I mention, and you may need to implement extra code for checking the unicity of keys for example.
At this stage, you will have to get a little more familiar with cell arrays, but then try for example to understand and use a containers.Map object. For this run/understand the example from the doc -> type in the command window:
doc containers.Map
Ben
on 20 Jul 2015
If all you need to implement is this Python-dict-like feature, yes. But if you really have to iterate through files and then use MATLAB for performing some type of processing, then you should stick to MATLAB. The last two mechanisms that I mention are close to Python dictionary objects; learning to use them is much easier than interfacing Python and MATLAB.
Ben
on 22 Jul 2015
Categories
Find more on Python Package Integration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!