Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Efficient general purpose 1D lookup table?
Replies: 7   Last Post: Jan 6, 2014 11:58 AM

 Messages: [ Previous | Next ]
 Paul Posts: 517 Registered: 2/23/10
Re: Efficient general purpose 1D lookup table?
Posted: Dec 6, 2013 6:37 PM

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 x-value inputs,
then I could have negative values to look up as well.

Steven:
I was in fact using a similar approach as your non-sparse 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.

Date Subject Author
12/5/13 Paul
12/6/13 EBS
12/6/13 Steven Lord
12/6/13 Paul
12/8/13 Steven Lord
12/11/13 Paul
12/11/13 Steven Lord
1/6/14 Paul