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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With