6

I am currently trying to map some data within a month depending on 'Month', 'week', 'day'.

This means that in principle I have three keys. I thought of following solutions to the problem:

1)

containers.Map(month,containers.Map(week,containers.Map(day,value)))

The problem with this solutions is to reference it I must use: How can I index a MATLAB array returned by a function without first assigning it to a local variable?.

And I find this solution rather "ugly".

2)

So another way would be to use a Map with multiple keys. The containers documentation show that only a single dimension key is allowed.

Question:

Do you have any trick to solve this multiple key problem?

Update/Solution:

I ended using a concatenated string as a key (as suggested) I added following piece of code to make the containers.Map as a proper multi-dimensional hashmap. Take a look below (I excluded week for simplification):

classdef example
properties
    myMap % Map for storage
end

methods

function obj = example()
        obj.myMap = containers.Map;
end

function obj2 = setVal(obj2,value,Month,DayType)
        key = strcat(num2str(Month),'-',num2str(DayType));
        obj2.myMap(key) = value;
    end

    function value = getValue(obj,Month,DayType)
        key = strcat(num2str(Month),'-',num2str(DayType));
       value = obj.myMap(key);
    end

end
Community
  • 1
  • 1
SteewDK
  • 363
  • 1
  • 4
  • 14
  • 1
    If it's month/week/day shoudn't convertig to a date (which is a double) be an option? Then you have a single key. – Daniel Jul 29 '14 at 09:38
  • @Daniel - I thought of that too, but I honestly don't know what format each key is in, so I went with a generic string. – rayryeng Jul 29 '14 at 13:36
  • 1
    @Daniel and rayryeng, take a look at the solution above. rayryeng once again thanks for your input! – SteewDK Jul 30 '14 at 13:17

2 Answers2

1

You could perhaps use all three of those keys to build a single key. I'm assuming this can be done as Month, week and day could be considered as unique. There is only one unique combination of these per occurrence. As such, simply take these keys and build them into single strings, then use theses as keys into your dictionary / containers.Map().

Here's an example:

%// Test data
month1 = 'May';
week1 = 2;
day1 = 'Thursday';

month2 = 'June';
week2 = 3;
day2 = 'Friday';

month3 = 'July';
week3 = 4;
day3 = 'Sunday';

%// Define keys
key1 = [month1 num2str(week1) day1];
key2 = [month2 num2str(week2) day2];
key3 = [month3 num2str(week3) day3];

%// Build dictionary
M = containers.Map();
M(key1) = 'Hello!';
M(key2) = 'Testing!';
M(key3) = 'Yes!';

%// Now test accessing
disp(M(key1));
disp(M(key2));
disp(M(key3));

The above code will take three months, three weeks and days, convert them into strings, and use these as keys into your dictionary. I don't know what the output type is for your purposes, so I just assigned strings. Note that I took the numbers and used num2str to convert the numbers into strings to ensure compatibility with the rest of the string. I don't know what data type week is (or any of the other variables in fact...), so simply use what I have and modify it for your own purposes.

I create the dictionary, then to test it, I access each of the values with each of the keys. As expected, my output is:

Hello!
Testing!
Yes!
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • @SteewDK - I made a lot of assumptions, so hopefully I'm on the right track somehow. If this doesn't help, then oops... at least I tried! – rayryeng Jul 29 '14 at 07:30
  • Thanks for your suggestion. I where not able to use a date as a key since this did not match dates. However, I used your method to constrstruct a function for getting and setting data in the Map - see my update above. Once again thanks alot! – SteewDK Jul 30 '14 at 13:07
0

There is a new MapNested class on Matlabs File-exchange: http://de.mathworks.com/matlabcentral/fileexchange/62492-mapnested-implementation-for-nested-maps--map-of-maps-

or on github:

https://github.com/RolandRitt/Matlab-NestedMap

Check it out!

The syntax for setting an value is the following:

NMapobj = MapNested(); %constructor;
NMapobj(key1, key2, key3) = value;

for retrieving:

value = NMapobj(key1, key2, key3);