在特定索引处连接两个多维 numpy 数组

Concatenating two multi-dimensional numpy arrays at specific index

我有两个大小为 numpy 的数组:[20,3,100,100] 和 [20,5,100,100]。

它们是 20 (100,100) 张图像,分别具有 3 个通道和 5 个通道。我想连接通道,以便我有 20 (100,100) 个图像和 8 个通道。 我想将它们沿着 (dim = 1) 连接在一起,而不必创建大小为 [20,8,100,100] 的新 numpy.zeros 数组。这可能吗?

Concatenate 沿现有轴合并数组:

import numpy as np

a = np.zeros((20,3,100,100))
b = np.ones((20,5,100,100))
output = np.concatenate((a,b), axis=1)

output.shape
# (20, 8, 100, 100)