python:添加2D和3D数组以创建新的3D(更大的三维)



我有两个不同的数组。A=[3124,5](表示3124个模型,具有5个参考参数(B=[3124,1912288](表示3124个模型,每个模型19个时间步长,每个时间步长12288个温度场数据点(

我想在每个时间步长将A(参数(数组中的相同5个值添加到温度场数组B的开头,这样我就得到了一个新的数组AB=[3124,1912293]。

我已尝试使用数据包AB = np.dstack((A, B)).shape

但我收到了错误消息ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 19

有人能帮我吗?

使用更适中的形状(您的B对我的机器来说太大了(:

In [4]: A = np.ones((3,4)); B = 2*np.ones((3,5,133))

我们可以扩展A以匹配:

In [5]: A[:,None,:].shape
Out[5]: (3, 1, 4)
In [6]: A[:,None,:].repeat(5,1).shape
Out[6]: (3, 5, 4)

现在数组在轴0和1上匹配,除了最后一个连接的数组:

In [7]: AB=np.concatenate((A[:,None,:].repeat(5,1),B),axis=2)
In [8]: AB.shape
Out[8]: (3, 5, 137)

这纠正了错误消息中提出的问题:

ValueError: all the input array dimensions for the concatenation 
axis must match exactly, but along dimension 1, the array at 
index 0 has size 5 and the array at index 1 has size 19

这样的东西会起作用:

import numpy
A = numpy.asarray([3124, 5])
B = numpy.asarray([3124, 19, 12288])
C = numpy.copy(B)
C[2] += A[1]
print(list(C))

output: [3124, 19, 12293]

然而,你并没有明确你的首要目标是什么。解决方案似乎比你想要的更直接。。。

最新更新