1

I have a 4d array to mask and a 2d array to mask index.

i should masking ndarray data using 2d array indices and i dont want to using for-loop. how should i do?

for example,

mask_arr = np.random.rand(5,10,100)
idx_arr = np.random.randint(10, size=(5,5))

and my masking code like this using for-loop

mask_val = 0
for i in range(5):
    mask_arr[i, idx_arr[i],:] = mask_val
buhtz
  • 10,774
  • 18
  • 76
  • 149
Young
  • 19
  • 3
  • 1
    Try `masking+arr[ np.arange(5), masking_idx] = mask_val` – hpaulj Jul 26 '22 at 16:56
  • oh! thanks to guide the direction. i tried to perform the operation, but just using np.arange(5) is not enough based numpy broadcasting rule . Array shape should be the same for broadcasting in indexing. `mask_arr[np.arange(5)[:,np.newaxis], idx_arr,:] = mask_val` this code work perfectly as indended. Thanks for the answer! – Young Jul 28 '22 at 11:58

0 Answers0