Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Paul

Posts: 263
Registered: 2/23/10
Re: Efficient general purpose 1D lookup table?
Posted: Dec 6, 2013 6:37 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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 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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.