将2D数组追加到3D数组,扩展三维 [英] Append 2D array to 3D array, extending third dimension
问题描述
我有一个形状为(480, 640, 3)
的数组A
和一个形状为(480, 640)
的数组B
.
I have an array A
that has shape (480, 640, 3)
, and an array B
with shape (480, 640)
.
如何将这两个附加为形状为(480, 640, 4)
的一个数组?
How can I append these two as one array with shape (480, 640, 4)
?
我尝试了np.append(A,B)
,但是它没有保持尺寸,而axis
选项导致了ValueError: all the input arrays must have same number of dimensions
.
I tried np.append(A,B)
but it doesn't keep the dimension, while the axis
option causes the ValueError: all the input arrays must have same number of dimensions
.
推荐答案
This handles the cases where the arrays have different numbers of dimensions and stacks the arrays along the third axis.
否则,要使用append
或concatenate
,您必须自己制作B
三维尺寸,并指定要连接它们的轴:
Otherwise, to use append
or concatenate
, you'll have to make B
three dimensional yourself and specify the axis you want to join them on:
>>> np.append(A, np.atleast_3d(B), axis=2).shape
(480, 640, 4)
这篇关于将2D数组追加到3D数组,扩展三维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!