【发布时间】:2018-01-18 07:49:20
【问题描述】:
我是 Python 的新手,目前正在编写一个代码,我想在其中存储 3 维矩阵的先前迭代,其版本是在 for 循环的每个步骤中创建的。我想解决这个问题的方法是连接一个维度为 3+1=4 的新数组,该数组存储以前的值。现在这可以通过连接实现,我让它像这样工作:
import numpy as np
matrix = np.ones((1,lay,row,col), dtype=np.float32)
for n in range(100):
if n == 0:
# initialize the storage matrix
matrix_stored = matrix
else:
# append further matrices in first dimension
matrix_stored = np.concatenate((matrix_stored,matrix),axis = 0)
所以这是我的问题:上面的代码要求矩阵已经是四维结构 [1 x m x n x o]。但是,出于我的目的,我更愿意将变量矩阵保留为三维 [m x n x o],并且仅在将其输入变量 matrix_stored 时将其转换为四维形式。
有没有办法促进这种转换?
【问题讨论】:
标签: python arrays python-3.x numpy concatenation