Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab map with multiple keys or nested

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
like image 685
SteewDK Avatar asked Jul 29 '14 06:07

SteewDK


1 Answers

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!
like image 157
rayryeng Avatar answered Nov 15 '22 16:11

rayryeng