Fast associative arrays or maps in Matlab
I need to build a fast one-to-one mapping between two large arrays of integers in Matlab. The mapping should take as input an element from a pre-defined array, e.g.:
in_range = [-200 2 56 45 ... ];
and map it, by its index in the previous array, to the corresponding element开发者_StackOverflow社区 from another pre-defined array, e.g.:
out_range = [-10000 0 97 600 ... ];
For example, in the case above, my_map(-200)
should output -10000
, and my_map(45)
should output 600
.
I need a solution that
- Can map very large arrays (~100K elements) relatively efficiently.
- Scales well with the bounds of
in_range
andout_range
(i.e. theirmin
andmax
values)
So far, I have solved this problem using Matlab's external interface to Java with Java's HashMaps, but I was wondering if there was a Matlab-native alternative.
Thanks!
The latest versions of Matlab have hashes. I'm using 2007b and they aren't available, so I use structs whenever I need a hash. Just convert the integers to valid field names with genvarname.
精彩评论