简体   繁体   中英

easy way to stack two 3D arrays

I have 2 arrays with the following shape:

array_1 (0,3,4)

and array_2 (0,1,4)

Whats the easiest way to stack or merge them together so I have a single array?

If you want an array of shape (0,4,4), you could append the arrays on the second axis:

>>> a = np.zeros((0,3,4))
>>> b = np.zeros((0,1,4))
>>> np.append(a, b, axis=1)
array([], shape=(0, 4, 4), dtype=float64)

After your comment, you want the first array to be in one row and the second to be in another . As they have different shapes, you will end with ragged nested sequences. This is currently deprecated but can be obtained by forcing an object dtype:

>>> x = np.empty(2, dtype=object)
>>> x[0] = a
>>> x[1] = b

After your comment to this question, you have:

a1 = np.zeros((2,3))
a2 = np.zeros((4,2))
a3 = np.zeros((1,4))

All you need is:

x = np.array((a1, a2, a3), object)

You can do this via the spread operator:

stacked = [*array_1, *array_2]
# result: [0, 3, 4, 0, 1, 4]

You can try np.hstack given it's dimesion 1 or np.append

import numpy as np

array_1  = np.array([0,3,4])
array_2  = np.array([0,1,4])

np.hstack((array_1, array_2)) # array([0, 3, 4, 0, 1, 4])

np.append(array_1, array_2) # array([0, 3, 4, 0, 1, 4])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM