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]])
user3000538
  • 189
  • 1
  • 2
  • 14

1 Answers1

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]])
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51