-2

I have 4 different numpy array's (2Dimension) and each array have the size (112,20). How can I convert (concatenate) them, to one array with 3 Dimension and the size of (112, 20, 4). Thanks for your support!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

0

Use np.stack((arr1, arr2, arr3, arr4), axis=2):

arr1 = np.zeros((2,5))
arr2 = np.ones((2,5))
arr3 = np.ones((2,5))*2
arr4 = np.ones((2,5))*3

v = np.stack((arr1, arr2, arr3, arr4), axis=2)

v.shape returns (2, 5, 4)

Output:

array([[[0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.]],

       [[0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.],
        [0., 1., 2., 3.]]])
T C Molenaar
  • 3,205
  • 1
  • 10
  • 26