The sample code is as below.
I want to get dataNew(h, w, length)
according to data(h, w, c)
and ind(h, w)
. Here length < c
, it means dataNew is sliced from data.
Here, length
and ind[i, j]
is made sure to suit the c
value.
I have realize it through for loops, but I wnat the python way. Please help, thanks!
import numpy as np
h, w, c = 3, 4, 5
data = np.arange(60).reshape((h, w, c))
print(data)
length = 3
ind = np.random.randint(0, 3, 12).reshape(h, w)
print(ind)
dataNew = np.empty((h, w, length), np.int16)
for i in range(h):
for j in range(w):
st = ind[i, j]
dataNew[i, j] = data[i, j][st : st + length]
print(dataNew)