Simple lookup table to translate values in array
Mostrar comentarios más antiguos
I am looking for a simple and fast way to translate values in an array using a lookup table. Given an array:
values = [ 103; 101; 102; ]
and a mapping
mapping = [ 101 5; 102 6; 103 7 ]
I would like to construct a function that returns translated values
lookup(values) % [ 7; 5; 6; ]
The following restrictions apply
- It should be fast and efficient enough to handle more than a million items with around 500 mapping entries in less than a few seconds and 1Gb of RAM (simply replicating the lookup table for every value requires like 100Gb of RAM).
- It shouldn't use any toolboxes that I don't have
- It should be one or two lines
The following does the right thing, but is too slow due to the slow lookup per element in the values array
lookup = @(values) arrayfun(@(value) mapping(mapping(:, 1) == value, 2), values)
Any ideas?
1 comentario
Jan
el 10 de Nov. de 2016
What is the benefit of doing this in one or two lines? This is a strange restriction if you want to get fast code.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Nonlinearity en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!