I have an array in three dimensions (a, b, c) and I need to modify the positions c indexed by an array in two dimensions (a, b).
I wrote a code that works as expected, but does anyone know if there is a way to solve this problem without the for loop?
import numpy as np
arr = np.random.randn(100, 5, 2)
mod = np.random.randn(100, 5)
ids = np.random.randint(0, 2, size=[100,5])
for i in range(100):
for j in range(5):
arr[i,j,ids[i,j]] = mod[i,j]
ids = np.random.randint(0, 2, size=[100,5])is helping you. It generates a 2D array filled randomly with 0s and 1s, which you then use to define which slicemod[i, j]gets assigned to. So in short when you assignmod[i, j], on your last line, you are assigning it randomly to point[i,j,0]or[i,j,1]Are you doing this on purpose? If so, I don't understand what you are trying to do so my answer probably isn't what you are looking for. If this isn't a bug, could you please edit your answer to clarify your question? Thanks.