1

I have a python function with the following signature:

def merge(segments, indexes):

where segments is a n-d numpy array and indexes is a one dimensional numpy array. Now, I want to call the following function:

np.where((segments == indexes[0]) | (segments == indexes[1]) | 
          ... segments == indexes[n])

However, I am not sure how I can generate this condition dynamically within the where() function call in python.

2
  • is n the length of indexes list? Commented Oct 16, 2014 at 14:19
  • No, it is dynamic, unfortunately. Commented Oct 16, 2014 at 14:37

1 Answer 1

1

Since you have many or conditions, you can use np.in1d() to check if each element of segments exists in indexes:

np.where(np.in1d(segments, indexes).reshape(segments.shape))

Note that the output of in1d() is a flattened array, needing to be reshaped such that where() will return the correct indices.

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.