1
    np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,9,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,10,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,12,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,11],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

I am trying to find how to take a numpy array like above and then in one performant operation mask it with indexes of elements I want zeroed

[0,1] [1,4] [4,7] [7,8] [8,11]

So what I am left with is

np.array(
[[0,0,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,0,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

Something like the functionality of np.in1d but for a 2d array? I can iterate over each element but the arrays can be truly massive so vector single operation mask would be best. Is it possible? If this is a stupid question I'm sure I'll be told!

2 Answers 2

2

You can directly access these indexes in the following way

indexes = [[0,1], [1,4], [4,7], [7,8], [8,11]]
indexes =zip(*indexes)
>>[(0, 1, 4, 7, 8), (1, 4, 7, 8, 11)]
a[indexes[0], indexes[1]]=0
>>
[[ 0  0  0  2  0  0  0  0  0  0  0  0]
 [ 0  0 15  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0 18  0  0  0  0  0  0]
 [ 0  0  0  0 27  0 20  0  0  0  0  0]
 [ 0  0  0  0  0 20  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  8  0  0  0]
 [ 0  0  0  0  0  0  0 14  0 14  0  0]
 [ 0  0  0  0  0  0  0  0  0  0 25  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0 15  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  7]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]]
Sign up to request clarification or add additional context in comments.

1 Comment

If you make indexes a tuple after zipping it, you can skip the indexing, i.e. indexes = tuple(zip(*indexes)); a[indexes] = 0.
1

I think you look for this

a = np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
 [0,0,15,0,9,0,0,0,0,0,0,0],
 [0,0,0,0,0,18,0,0,0,0,0,0],
 [0,0,0,0,27,0,20,0,0,0,0,0],
 [0,0,0,0,0,20,0,10,0,0,0,0],
 [0,0,0,0,0,0,0,0,8,0,0,0],
 [0,0,0,0,0,0,0,14,0,14,0,0],
 [0,0,0,0,0,0,0,0,12,0,25,0],
 [0,0,0,0,0,0,0,0,0,0,0,11],
 [0,0,0,0,0,0,0,0,0,0,15,0],
 [0,0,0,0,0,0,0,0,0,0,0,7],
 [0,0,0,0,0,0,0,0,0,0,0,0]])

b = np.array([[0,1],[1,4],[4,7],[7,8],[8,11]])

# get x coordinates in an array
c1 = b[:,0]
# get y coordinates in an array
c2 = b[:,1]
a[c1[:,None],c2] = 0

a 
array([[ 0,  0,  0,  2,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0, 18,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0, 27,  0, 20,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0, 20,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0, 14,  0, 14,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 25,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 15,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  7],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0]])

1 Comment

Once you turn b into an array, a[tuple(b.T)] = 0 is likely going to be the most compact way of getting things done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.