필터 지우기
필터 지우기

Simple lookup table to translate values in array

조회 수: 12 (최근 30일)
Jetze Sikkema
Jetze Sikkema 2016년 11월 10일
댓글: Jetze Sikkema 2016년 11월 11일
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
Jan
Jan 2016년 11월 10일
What is the benefit of doing this in one or two lines? This is a strange restriction if you want to get fast code.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2016년 11월 10일
편집: Jan 2016년 11월 10일
mapping = [ 101 5; 102 6; 103 7 ];
values = [ 103; 101; 102];
% Inflate the look up table:
% LUT = zeros(1, max(mapping(:, 1))); % Not required
LUT(mapping(:, 1)) = mapping(:, 2);
result = LUT(values);
This works efficiently if the lookup data are not too sparse. You can subtract min(mapping)+1 from the values and the mapping.
Or is this the method you call "simply replicating the lookup table for every value"? If so, please post realistic values.
  댓글 수: 1
Jetze Sikkema
Jetze Sikkema 2016년 11월 11일
Hi Jan,
Thanks for the answer. It was not what I was looking for, as you also generate entries for all integers from 1 up to the maximum key. However it does very effectively solve my current problem since my keys conveniently go go from 1 to 278 (i.e. they are already encoded country names). Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Nonlinearity에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by