Associate Map container with specific values
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi seniors,
I've been struggling finding a way to associate a map container with a specific value. For instance, we have four states [5 7 19 8] as well as four map containers. Each map container is associated with one of the states. Ideally, what I want is when I'm at state 5, I can access the map it's associated with. In other words, 5 is a key to a map container. However, I'm aware that map container is not a valid value type for container.Map.
So, please suggest a good way of doing this. Appreciate it!
0 Kommentare
Antworten (3)
Peter O
am 1 Sep. 2020
Map containers are a type of hash object, but not all MATLAB datatypes are hashable. The keys of containers are limited to a subset of types MATLAB can guarantee will hash correctly. To use named states (which is what I think you're after), consider using an enumeration object to define the states and cast them to string to access into the map. Unfortunately, MATLAB doesn't yet seem to support enums as hash accessors directly yet
As an example
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
Then to access:
mymap = containers.Map();
mystate = WeekDays.Tuesday;
mymap(string(WeekDays.Tuesday)) = 'Data!' % Or your favorite data structure.
mymap(string(mystate))
>>> 'Data!'
In my limited experimentation from circa 2014, Map objects can be slower to access than other structures, and the string conversion overhead of an enum object may also factor in. But give it a try! If you're not writing high performance code or something called 10,000 times, it is likely fine for your application.
Steven Lord
am 1 Sep. 2020
As long as your states are positive integer values and the largest one isn't too large, you could make a cell array and store each of your containers.Map objects in the corresponding cell in the cell array in a similar way that I'm storing magic matrices in each of four cells in this example.
M = randperm(20, 4);
C = cell(1, 20);
for k = M
C{k} = magic(k);
end
celldisp(C)
Mohammad Alhashash
am 25 Sep. 2021
You store these Map containers into cell array of structure and create another Map container for mapping states into their corresponding index values.
Using cell array for example:
% creating 4 arbitrary Map containers
A = containers.Map ();
B = containers.Map ();
C = containers.Map ();
D = containers.Map ();
% store them into cell array container called X
X = {A, B, C, D};
% Create a mapping key/value pairs to map state into a specific index
state = containers.Map([5 7 19 8], [1 2 3 4]);
% Calling the Map container asscoiate with state 19 for example
X{state(5)}
Or you can map the state into the corresponding Map container name directly and use eval function:
% Create an new mapping function
state = containers.Map([5 7 19 8], {"A", "B", "C", "D"});
% Calling the Map container associated with state 19 for example
eval(state(19))
1 Kommentar
per isakson
am 26 Sep. 2021
Bearbeitet: per isakson
am 26 Sep. 2021
"However, I'm aware that map container is not a valid value type for container.Map." Isn't it? This works on my R2018b. Isn't this very close to what you asks for?
%%
keySet = {'Jan','Feb','Mar','Apr'};
valueSet = [11,12,13,14];
M1 = containers.Map( keySet, valueSet );
valueSet = [21,22,23,24];
M2 = containers.Map( keySet, valueSet );
valueSet = [31,32,33,34];
M3 = containers.Map( keySet, valueSet );
valueSet = [41,42,43,44];
M4 = containers.Map( keySet, valueSet );
%%
S = containers.Map( [5,7,8,19], {M1,M2,M3,M4} )
%%
cur = S(5);
cur('Feb')
cur = S(19);
cur('Apr')
% S(8)('Jan') % throws an error
Siehe auch
Kategorien
Mehr zu Dictionaries finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!