6

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.

1
  • this question was first so I don't want to flag it as a duplicate, but there's another identical question with more votes and a slightly better answer (also from Alex Riley): stackoverflow.com/questions/34357617/… Commented Sep 5, 2019 at 0:03

1 Answer 1

10

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).

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

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.