简体   繁体   中英

Concatenating multi-dimensional arrays in Python

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM