4

I'm searching for an algorithm to merge a given number of multidimensional arrays (each of the same shape) to a given proportion (x,y,z).

For example 4 arrays with the shape (128,128,128) and the proportion (1,1,4) to an array of the shape (128,128,512). Or 2 arrays with the shape (64,64,64) and the proportion (1,2,1) to an array of the shape (64,128,64)

I know how to do it manually with np.concatenate, but I need a general algorithm to do this. (np.reshape doesn't work - this will mess up the order)

edit: It's possible that the proportion is (1,2,3), then it is necessary to compare the left_edge of the box, to know where to place it. every array have a corresponding block with the attribute left_edge (xmin, ymin, zmin). Can I solve this with a if-condition?

4

2 回答 2

1

If your proportion is always one-dimensional (i.e. concatenate in one dimension only), you can use this:

arrays = [...]
proportion = (1,1,4)

np.concatenate(arrays, axis=next(i for i,p in enumerate(proportion) if p>1))

Otherwise you have to explain what to do with proportion = (1,2,3)

于 2013-01-04T09:34:58.503 回答
0

好的,我以这种方式对其进行了编程,并且似乎可以正常工作。也许不是最好的方式,但它做我想要的。

blocks.sort(key=lambda x: (x.left_edge[2],x.left_edge[1],x.left_edge[0]))
proportion = (Nx * nblockx, Ny * nblocky, Nz * nblockz)

arrays = np.zeros((nblockx, nblocky, nblockz, Nx, Ny, Nz))

for block, (x,y,z) in zip(root_list,
                          product(range(nblockx),
                                  range(nblocky),
                                  range(nblockz))):
    array = np.zeros((Nx, Ny, Nz), dtype = np.float64)

    # this is only the function to fill the array
    writearray(array, ...)

    arrays[x,y,z] = array

shape = arrays.shape
array = np.zeros((shape[0]*shape[3], shape[1]*shape[4], shape[2]*shape[5]))
for x,y,z in product(range(shape[0]), range(shape[1]), range(shape[2])):
    slicex = slice(x*shape[3], (x+1)*shape[3])
    slicey = slice(y*shape[4], (y+1)*shape[4])
    slicez = slice(z*shape[5], (z+1)*shape[5])

    array[slicex, slicey, slicez] = arrays[x,y,z]

return array
于 2013-01-04T11:32:00.553 回答