如何添加新行和列来填充 numpy 数组
How to add new rows and columns to pad numpy array
我有一个矩阵数组,所有矩阵都不同 sizes/dimensions,我需要将它们全部填充到相同的大小(805、16866)。 Bellow 是我目前用来执行此操作的代码,但我相信它可以被矢量化,但我不确定如何执行此操作。
x1 = np.zeros((805, 16866))
for i in range(x[0].shape[0]):
for j in range(x[0].shape[1]):
x1[i, j] = x[0][i, j]
在上面的示例中,x1[:x[0].shape[0], :x[0].shape[1]] = x
应该有效
numpy.pad
函数 (docs) 可以满足您的需求。
您要的密码是
x1 = np.zeros((805,16866))
for i in range(x.shape[0])
x1[i,:]= np.pad(x[i,:],x1.shape[0],'constant',constant_values=0)
我有一个矩阵数组,所有矩阵都不同 sizes/dimensions,我需要将它们全部填充到相同的大小(805、16866)。 Bellow 是我目前用来执行此操作的代码,但我相信它可以被矢量化,但我不确定如何执行此操作。
x1 = np.zeros((805, 16866))
for i in range(x[0].shape[0]):
for j in range(x[0].shape[1]):
x1[i, j] = x[0][i, j]
在上面的示例中,x1[:x[0].shape[0], :x[0].shape[1]] = x
应该有效
numpy.pad
函数 (docs) 可以满足您的需求。
您要的密码是
x1 = np.zeros((805,16866))
for i in range(x.shape[0])
x1[i,:]= np.pad(x[i,:],x1.shape[0],'constant',constant_values=0)