Paul
Posts:
513
Registered:
2/23/10


Re: Efficient general purpose 1D lookup table?
Posted:
Dec 6, 2013 6:37 PM


Eric asked for an example.
Steven wrote: > locations = 1:10 > values = locations.^2 > > interpolationLocations = randi([1 10], 1, 20) > interpolatedValues = values(interpolationLocations) > > % If some of the first set of unique integers can be large, consider > % using a sparse column vector as your table. > > values = 0:6 > locations = 10.^(values) > lookupTable = sparse(locations, 1, values) > > valuesToFind = 10.^randi([0, 6], 20, 1) > foundValues = full(lookupTable(valuesToFind, 1))
Eric, Steven,
Eric: One example lookup table might be: x=unique(randperm(20,10)); y=randperm(20,10)10; [x' y']
I would be translating any occurance of a value in the left column into the corresponding value in the right column. I want to the translation to be vectorizable. The x values are not contiguous, so I *could* do
mymap=NaN(1,max(x)) mymap(x)=y % Now do vectorized conversion mymap(x(randperm(length(x))))
If I didn't rely on the index of mymap to match the xvalue inputs, then I could have negative values to look up as well.
Steven: I was in fact using a similar approach as your nonsparse lookup table. Since the allowable input values are not continguous, it would be nice if the input argument didn't have to be continuous (matrix indexes have to be). The sparse matrix approach almost does it, but I noticed the "full" command, which blows it up into a full matrix. And it would be good if I could cause some kind of abnormality like use NaNs in place of the zeros in a sparse matrix.
Currently, I avoid the issue by restricting myself to contigous input values starting from 1. Strictly speaking, I could encounter situations where that isn't the case, so I was hoping to write code that handles those situations without adding to much to the code (and ideally without departing from vectorization).
Thank you both for your ideas.

