The conditions are following:
1) we have a list of N-D arrays and this list is of unknown length M
2) dimensions each arrays are equal, but unknown
3) each array should be splitted along 0-th dimension and resulting elements should be grouped along 1-st dimension of length M and then stacked back along 0-th dimension of the same length it was
4) resulting rank should be N+1 and the lenght of 1-st dimension should be M
Above is the same as zip, but in the world of N-D arrays.
Currently I do the following way:
xs = [list of numpy arrays]
grs = []
for i in range(len(xs[0])):
gr = [x[i] for x in xs]
gr = np.stack(gr)
grs.append(gr)
grs = np.stack(grs)
Can I write shorter with bulk operations?
UPDATE
Here is what I want
import numpy as np
sz = 2
sh = (30, 10, 10, 3)
xs = []
for i in range(sz):
xs.append(np.zeros(sh, dtype=np.int))
value = 0
for i in range(sz):
for index, _ in np.ndenumerate(xs[i]):
xs[i][index] = value
value += 1
grs = []
for i in range(len(xs[0])):
gr = [x[i] for x in xs]
gr = np.stack(gr)
grs.append(gr)
grs = np.stack(grs)
print(np.shape(grs))
This code apparantly works correctly, producing arrays of shape (30, 2, 10, 10, 3). Is it possible to avoid loop?
Seems you need to transpose the array with respect to its 1st and 2nd dimension; You can use
swapaxesfor this:Example: