Pazu
Pazu

Reputation: 287

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.

Upvotes: 1

Views: 1135

Answers (3)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

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

Upvotes: 1

tstanisl
tstanisl

Reputation: 14127

You could use slicing to select even columns and boolean indexing to set zeros.

f_2[::2][f_1==0]=0

Upvotes: 2

pecey
pecey

Reputation: 681

Why not simply do something like this?

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

Upvotes: 1

Related Questions