views:

36

answers:

1

I have a bit of code that attempts to find the contents of an array at indices specified by another, that may specify indices that are out of range of the former array.

input = np.arange(0, 5)
indices = np.array([0, 1, 2, 99])

What I want to do is this: print input[indices] and get [0 1 2]

But this yields an exception (as expected):

IndexError: index 99 out of bounds 0<=index<5

So I thought I could use masked arrays to hide the out of bounds indices:

indices = np.ma.masked_greater_equal(indices, 5)

But still:

>print input[indices]
IndexError: index 99 out of bounds 0<=index<5

Even though:

>np.max(indices)
2

So I'm having to fill the masked array first, which is annoying, since I don't know what fill value I could use to not select any indices for those that are out of range:

print input[np.ma.filled(indices, 0)]

[0 1 2 0]

So my question is: how can you use numpy efficiently to select indices safely from an array without overstepping the bounds of the input array?

+3  A: 

Without using masked arrays, you could remove the indices greater or equal to 5 like this:

print input[indices[indices<5]]

Edit: note that if you also wanted to discard negative indices, you could write:

print input[indices[(0 <= indices) & (indices < 5)]]
François
D'oh that works. I'm still curious about why we can't properly use masked arrays for indexing, but I suppose it doesn't really matter.
Widjet