Reputation: 21390
It is possible to index a numpy array with a tuple of sequences such that tpl[0]
is a a sequence of x coordinates and tple[1]
is a sequence of y coordinates. One simply needs to index the array with the tuple, thus: other_array[tpl]
.
I currently have coordinates stored in a 2D array such that the vector ar[0]
corresponds to my x values and ar[1]
corresponds to my y values.
Right now, I'm indexing other_array
by creating a tuple: other_array((ar[0], ar[1]))
. Unfortunately, this operation is running in a tight loop, so any amount of performance I can squeeze out would be highly beneficial. Creating a tuple can add a bit of overhead if performed 10^8 times! Is there a faster, numpythonic way of indexing with such a matrix of xy coordinates?
Thank you very much!
Upvotes: 2
Views: 1222
Reputation: 114781
You can index a numpy array with another array, so you don't have to create a tuple. For example:
In [199]: other_array
Out[199]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [200]: ar
Out[200]:
array([[0, 2, 1],
[1, 3, 0]])
In [201]: other_array[ar[0], ar[1]]
Out[201]: array([ 1, 13, 5])
If that doesn't answer your question, could you include a simple working example in your question that shows what you are currently doing?
Upvotes: 1