The Math Forum

Search All of the Math Forum:

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

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

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

Advanced Search

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

Posts: 517
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,

One example lookup table might be:
[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

% Now do vectorized conversion

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.

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]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.