I have an array that specifies how many elements along the last axis of a matrix I want to change to 0. How do I do this efficiently?
x = np.ones((4, 4, 10))
change_to_zeros = np.random.randint(10, size=(4, 4))
# change_to_zeros =
# [[2 1 6 8]
# [4 0 4 8]
# [7 6 6 2]
# [4 0 7 1]]
Now what I want to do is something like x[:, :, :change_to_zeros] = 0 - for example for the first element of change_to_zeros, I have change_to_zeros[0, 0] = 2 so I want to change the first (or last, or whatever) 2 elements along the last axis of x (length 10) to 0.
Clarification: For example at x[0, 0, :] I have ones with length 10. I want to change 2 of these (change_to_zeros[0, 0] = 2) to 0, keeping the rest with ones.
x[0, 0, :]I have ones with length 10. I want to change 2 of these (change_to_zeros[0, 0] = 2) to 0, keeping the rest with ones.