Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Matlab, can I access an element of an array, which is in turn a value of a container.Map?

Here is a code snippet, that shows what I want and the error, that follows:

a = [1, 2];
m = containers.Map('KeyType','char', 'ValueType','any');
m('stackoverflow.com') = a;
pull_the_first_element_of_the_stored_array = m('stackoverflow.com')(1);
??? Error: ()-indexing must appear last in an index expression.

How do I access an element of the array, which is in turn a value of a map object? I could have done this:

temp = m('stackoverflow.com');
pull_the_first_element_of_the_stored_array = temp(1);

But I do not want to create an intermediate array only to pull a single value out of it.

EDIT : This is a duplicate of How can I index a MATLAB array returned by a function without first assigning it to a local variable? The answer is there.

like image 425
Anton Daneyko Avatar asked Feb 20 '12 16:02

Anton Daneyko


People also ask

What is containers map in Matlab?

Objects with keys that index to values, where keys need not be integers. Store data values in a Map object, which is a data structure that associates each value with a corresponding key. A Map object is similar to a dictionary or associative array in that you can use keys to retrieve values from it.

Does Matlab have HashMap?

MATLAB HashMap Implementation Some data types are not supported at all (structs, function handles, etc). This implementation gives you more of a modern implementation of hash maps, allowing mixing of data types, functions/structs/classes as map keys, and concise language to improve code readability.

Is there a map function in Matlab?

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. 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 does map mean in Matlab?

A Map is a type of fast key lookup data structure that offers a flexible means of indexing into its individual elements.


2 Answers

This is another case where you can get around syntax limitations with small helper functions. EG:

getFirst = @(x)x(1);

pull_the_first_element_of_the_stored_array = getFirst(m('stackoverflow.com'));

This still needs two lines, but you can often reuse the function definition. More generally, you could write:

getNth = @(x, n) x(n);

And then use:

getNth (m('stackoverflow.com'),1);
like image 101
Pursuit Avatar answered Oct 31 '22 03:10

Pursuit


Although this question is a duplicate of this previous question, I feel compelled to point out one small difference between the problems they are addressing, and how my previous answer could be adapted slightly...

The previous question dealt with how to get around the syntax issue involved in having a function call immediately followed by an indexing operation on the same line. This question instead deals with two indexing operations immediately following one another on the same line. The two solutions from my other answer (using SUBSREF or a helper function) also apply, but there is actually an alternative way to use SUBSREF that combines the two indexing operations, like so:

value = subsref(m,struct('type','()','subs',{'stackoverflow.com',{1}}));

Note how the sequential index subscripts 'stackoverflow.com' and 1 are combined into a cell array to create a 1-by-2 structure array to pass to SUBSREF. It's still an ugly one-liner, and I would still advocate using the temporary variable solution for the sake of readability.

like image 24
gnovice Avatar answered Oct 31 '22 02:10

gnovice