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!
Asked
Active
Viewed 81 times
-2
-
`c = np.stack((a, b, c, d), axis=2)` – Trenton McKinney Aug 11 '22 at 18:04
-
1Does this answer your question? [Concatenate two numpy arrays in the 4th dimension](https://stackoverflow.com/questions/8898471/concatenate-two-numpy-arrays-in-the-4th-dimension) – Trenton McKinney Aug 11 '22 at 18:04
1 Answers
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
-
1add an `axis` parameter to get his desired shape. I like to 'prove' my answers with an example. – hpaulj Aug 11 '22 at 14:34
-
-