Search code examples
pythonarraysnumpyzero

Setting an array to zero on a specified list of indices


Let f_1 be an array of shape (a,b) and f_2 an array of shape (2*a,b). f_2 equals f_1 if we restrict the array on even rows. I want to set f_2 to zero to everywhere where f_1 is zero. I tried the following:

I = np.argwhere(f_1 == 0)

for x in I:
    x[0] = 2*x[0]

f_2[I] = 0

but LA.norm(f_2) results in 0.0 so this approach apparently doesn't work.


Solution

  • You can multiply the indices by two for the first dimension with:

    i,j = np.where(f_1 == 0)
    f_2[2*i,j] = 0