In Python, say you have a 2D array of zeros of shape (N,4,4):
z = array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
and you have a 2D array of indices:
i = array([[1, 1, 1, 1],
[1, 0, 0, 0],
[0, 0, 1, 1],
[1, 1, 1, 0]])
and some valued 2D array:
v = array([[ 2., 4., 10., 7.],
[10., 9., 9., 2.],
[ 3., 8., 8., 8.],
[ 8., 6., 10., 1.]])
Is there a way to fill the elements of z with the values of v but in the slices denoted by i, without using loops?
Note: Is there a way to do this in a scalable fashion such that if you had an N channel array z, where N>>1, you would not need to directly index z[i] when filling it with values from v?
For clarity, the resulting z array would look like the following:
z = array([[[0., 0., 0., 0.],
[0., 9., 9., 2.],
[3., 8., 0., 0.],
[0., 0., 0., 1.]],
[[2., 4., 10., 7.],
[10., 0., 0., 0.],
[0., 0., 8., 8.],
[8, 6., 10., 0.]]])
Obviously, using something like:
z = v[i,:,:]
would never work but maybe there's a way to use rows, cols to do this?
Many thanks in advance!
Edit:
For clarity here is a similar example but for a 3D z as requested in the comments:
z = array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
i = array([[2, 1, 2, 1],
[1, 1, 2, 1],
[1, 1, 1, 1],
[1, 0, 0, 1]])
v = array([[5., 5., 0., 4.],
[4., 6., 8., 3.],
[4., 0., 4., 8.],
[7., 6., 5., 7.]])
z would become:
z = array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 6., 5., 0.]],
[[0., 5., 0., 4.],
[4., 6., 0., 3.],
[4., 0., 4., 8.],
[7., 0., 0., 7.]],
[[5., 0., 0., 0.],
[0., 0., 8., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])