Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach two 3d numpy arrays in Python

I was wondering how I can attach two 3d numpy arrays in python?

For example, I have one with shape (81,81,61) and I would like to get instead a (81,81,122) shape array by attaching the original array to itself in the z direction.

like image 273
aregak Avatar asked Mar 17 '23 02:03

aregak


1 Answers

One way is to use np.dstack which concatenates the arrays along the third axis (d is for depth).

For example:

>>> a = np.arange(8).reshape(2,2,2)
>>> np.dstack((a, a))
array([[[0, 1, 0, 1],
        [2, 3, 2, 3]],

       [[4, 5, 4, 5],
        [6, 7, 6, 7]]])

>>> np.dstack((a, a)).shape
(2, 2, 4)

You could also use np.concatenate((a, a), axis=2).

like image 118
Alex Riley Avatar answered Mar 28 '23 05:03

Alex Riley