1

Is it possible to simple use an array like:

import numpy as np
a = np.array([1,76,120,158,199]

as an condition array for another array?

I have for example an array like:

b=np.array([0,1,2,3,4,5,76,77,78,79,120,121,122,158,159,199,200,201])

All numbers of array b that are also in array a should get the number 1 for example:

return:

c=np.array([0,1,2,3,4,5,1,77,78,79,1,121,122,1,159,1,200,201])

So is there a fast numpy version to do this. Because the actual array is a huge 3d array with many numbers, which of course could be flattened.

0

1 Answer 1

2

Using numpy.in1d and Boolean indexing:

b[np.in1d(b, a)] = 1

print(b)

array([  0,   1,   2,   3,   4,   5,   1,  77,  78,  79,   1, 121, 122,
         1, 159,   1, 200, 201])
Sign up to request clarification or add additional context in comments.

Comments

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.