2

My question is inspired by another one: Intersection of 2d and 1d Numpy array I am looking for a succinct solution that does not use in1d

The setup is this. I have a numpy array of bools telling me which values of numpy array A I should set equal to 0, called listed_array. However, I want to ignore the information in the first 3 columns of listed_array and only set A to zero as indicated in the other columns of listed_array.

I know the following is incorrect:

A[listed_array[:, 3:]] = 0

I also know I can pad this subset of listed_array with a call to hstack, and this will yield correct output, but is there something more succinct?

2
  • Do A and listed_array have the same shape? Commented Sep 9, 2015 at 16:02
  • And if so, does that mean you don't want to change anything in the first three columns of A? Commented Sep 9, 2015 at 16:07

1 Answer 1

1

If I understand the question, this should do it:

A[:, 3:][listed_array[:, 3:]] = 0

which is a concise version of

mask3 = listed_array[:, 3:]
A3 = A[:, 3:]   # This slice is a *view* of A, so changing A3 changes A.
A3[mask3] = 0
Sign up to request clarification or add additional context in comments.

1 Comment

yes of course! I feel silly having missed this. thank you.

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.