将2D数组追加到3D数组,扩展三维 [英] Append 2D array to 3D array, extending third dimension

查看:95
本文介绍了将2D数组追加到3D数组,扩展三维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为(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.

否则,要使用appendconcatenate,您必须自己制作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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆