Jan Weidner
Jan Weidner

Reputation: 589

julia fast lookup of a list of array values

I have a lookup table in the form of a 2d array and a list of indices (in the form of two 1d arrays xs, ys) at which I would like to evaluate the lookup table. How to accomplish this in a fast manner?

It looks like a standard problem, however I found nothing about looking up array values at a general list of indices (e.g. not a cartesian product) in the docs. I tried

result = zeros((10^6,))
for i in [1:10^6]
    x = xs[i]
    y = ys[i]
    result[i] = lookup[x, y]
end

Besides looking a bit cumbersome, this code is also 10 times slower then an equivalent numpy code. Also it looks like a standard problem, however I found nothing about looking up array values at a general list of indices (e.g. not a cartesian product) in the docs.

So what would be a fast alternative to the above code?

Upvotes: 1

Views: 1459

Answers (2)

Thomas Dickson
Thomas Dickson

Reputation: 363

Here are the updated links for Base.getindex (see https://docs.julialang.org/en/v1/base/collections/#Base.getindex). The broadcasted implementation found here.

Upvotes: 1

Toivo Henningsson
Toivo Henningsson

Reputation: 2699

You can try broadcast_getindex (see http://julia.readthedocs.org/en/latest/stdlib/arrays/#Base.broadcast_getindex).

Otherwise, it looks like your code should be pretty efficient if you just change [1:10^6] to 1:10^6.

Upvotes: 2

Related Questions