1

I'd need to know the most efficient way for the following case. There is a numpy.ndarray of shape 11k*11k for which I need to force all elements of some rows to be zero given a binary numpy array of shape 11k. A toy example could be described as follows:

Inputs:

x = np.array([[2, 1, 1, 2],
              [0, 2, 1, 0],
              [1, 0, 1, 1],
              [2, 2, 1, 0]])

ref = np.array([0, 1, 1, 0])

Output:

y = ([[0, 0, 0, 0],
       [0, 2, 1, 0],
       [1, 0, 1, 1],
       [0, 0, 0, 0]])
1
  • 2
    Extend ref to 2D and multiply - x*ref[:,None]. Commented Jun 17, 2019 at 16:57

1 Answer 1

2

Use this -

y = np.multiply(x.T,ref).T
array([[0, 0, 0, 0],
       [0, 2, 1, 0],
       [1, 0, 1, 1],
       [0, 0, 0, 0]])
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.