Skip to content

Instantly share code, notes, and snippets.

@andrefreitas
Created May 12, 2013 09:11
Show Gist options
  • Save andrefreitas/5562913 to your computer and use it in GitHub Desktop.
Save andrefreitas/5562913 to your computer and use it in GitHub Desktop.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment