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?

2
  • "2 arrays with the shape (64,64)" - is their shape (64,64,64)? Commented Jan 4, 2013 at 9:36
  • sorry, my fault - you are right. Commented Jan 4, 2013 at 10:16

2 Answers 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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. I edited my question, cause there are more complicate proportions.
0

Okay I programmed it this way and it seems to work. Maybe not the nicest way, but it do what I want.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.