1

I have arrays

A = np.array([
  [1,6,5],
  [2,3,4],
  [8,3,0]
])

B = np.array([
  [3,2,1],
  [9,2,8],
  [2,1,8]
])

Doing a = np.argwhere(A > 4) gives me an array of position/indexes of values in array A that are greater than 4 i.e. [[0 1], [0 2], [2 0]].

I need to use these indexes/position from a = np.argwhere(A > 4) to replace the values in array B to zero at these specific position i.e. array B should now be

B = np.array([
  [3,0,0],
  [9,2,8],
  [0,1,8]
])

I am big time stuck any help with this will be really appreciated.

Thank You :)

4
  • 1
    B[A > 4] = 0? Commented Jan 14, 2018 at 2:24
  • I don't want to threshold B, I want to threshold A and positions/indexes that are has value less than the threshold has to be made zero in array B. Commented Jan 14, 2018 at 2:35
  • 1
    That's why you take the mask for A and index it with B. What's the confusion? It gives you what you want. Commented Jan 14, 2018 at 2:36
  • Apologies for the simple question .. I am pretty new to python. Thank you.. Commented Jan 14, 2018 at 2:40

2 Answers 2

2

It should be as simple as:

B[A > 4] = 0
Sign up to request clarification or add additional context in comments.

Comments

2

In general, though, note that the indices returned by np.where are meant to be applied to numpy.ndarray objects, so you could have done:

B[np.where(A > 4)] = 0

Generally I don't use np.where with a condition like this, I just use the boolean mask directly, as in John Zwinck's answer. But it is probably important to understand that you could

>>> B[np.where(A > 4)] = 0
>>> B
array([[3, 0, 0],
       [9, 2, 8],
       [0, 1, 8]])

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.