-1

Let's say I have two arrays of shape (3,2,3)

a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
b = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
a.shape
b.shape

I would like to join these two arrays by adding a new dimention to get (2,3,2,3) like this:

c = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]])
c.shape

How would I do this?

Stata_user
  • 562
  • 3
  • 14

1 Answers1

1

You can use numpy.stack (Join a sequence of arrays along a new axis.) on axis=0 base on concatenate and add a new dimension on the first dimension.

c = np.stack((a, b), axis=0)
print(c.shape)
# (2, 3, 2, 3)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30