Home > OS >  Concatenating multi-dimensional arrays in Python
Concatenating multi-dimensional arrays in Python

Time:06-20

I have two arrays C1 and C2 with dimensions (1, 2, 2) . I want to append the arrays into a new array C3. The current and desired outputs are attached.

import numpy as np

arC1 = []
arC2 = []

C1=np.array([[[0, 1],[0,2]]])
arC1.append(C1)
C2=np.array([[[1, 1],[2,2]]])
arC2.append(C2)

C3=np.append(C1,C2)

The current output is

array([0, 1, 0, 2, 1, 1, 2, 2])

The desired output is

array([[[0, 1],[0,2]],[[1, 1],[2,2]]])

C3[0]=array([[0, 1],
       [0, 2]])

C3[1]=array([[1, 1],
       [2, 2]])

CodePudding user response:

You can use axis parameter of function append.

print(np.append(C1, C2))
"""
[0 1 0 2 1 1 2 2]
"""
print(np.append(C1, C2, axis=1))
"""
[[[0 1]
  [0 2]
  [1 1]
  [2 2]]]
"""
print(np.append(C1, C2, axis=0))
"""
[[[0 1]
  [0 2]]
 [[1 1]
  [2 2]]]
"""

I think np.append(C1, C2, axis=0) is what you want.

  • Related