Created
May 12, 2013 09:11
-
-
Save andrefreitas/5562913 to your computer and use it in GitHub Desktop.
Exemplo da utilização de Maps em matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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