andrefreitas
5/12/2013 - 9:11 AM

Exemplo da utilização de Maps em matlab

Exemplo da utilização de Maps em matlab

% Using Maps to have constant time complexity to access data
% See more at http://www.mathworks.com/help/matlab/map-containers.html
% Example here http://stackoverflow.com/questions/3591942/hash-tables-in-matlab
% Let's supose we have the following information:

table =[ [1 , 1 , 1 , 0.444 , 0.67 , 0.98];
         [1 , 1 , 2 , 0.254 , 0.67 , 0.98]
       ];
   
% So to store them in an efficient way
% To convert [1,1,1] to '111' just do  [num2str(1), num2str(1), num2str(1)]
keys = {'111', '112'};
values = {[0.444 , 0.67 , 0.98], [0.254 , 0.67 , 0.98]};
data = containers.Map(keys, values)

% Acess a value: 
data('111')

% ---- or this is the same
data([num2str(1), num2str(1), num2str(1)])

% Creating a new key-value pair
data('222') = [0.56, 0.67 , 0.86]

% Conclusion:
% In this way we don't need to do a for to iterate all over the data, since
% this way is faster.