問題描述:array升維 eg:(128,128,4)加一層(128,128,1) 變成 (128,128,5)
I have an array A
that has shape (480, 640, 3)
, and an array B
with shape (480, 640)
.
How can I append these two as one array with shape (480, 640, 4)
?
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
.
解決方案:
>>> a = np.arange(8).reshape(2,2,2)
>>> np.dstack((a, a))
array([[[0, 1, 0, 1],
[2, 3, 2, 3]],
[[4, 5, 4, 5],
[6, 7, 6, 7]]])
>>> np.dstack((a, a)).shape
(2, 2, 4)