【问题标题】:create a cell or contained of matrix of different size with in a loop in python在python的循环中创建一个单元格或包含不同大小的矩阵
【发布时间】:2020-05-22 16:20:07
【问题描述】:

我想在循环中创建一个包含不同大小矩阵的容器,作为单元格或列表。我创建了一个这样的python代码:

W = []
W_tot = []
for i in range(0,layer_number):   #layer_numer = number of matrix to be created
    row_W = int(input('NumRow = ')) #number of row of the nth matrix
    col_W = int(input('Numcols = ')) #number of cols of the nth matrix
    W = 0.5*np.random.rand(row_W,col_W) #creation of the nth matrix with random number
    cells = np.array([W]) #insert of the created matrix in a cell

无论如何,我总是出错。当然,我在创建一个包含循环每一步创建的所有矩阵的单元格时犯了一个错误。

【问题讨论】:

  • 请包含错误
  • 我没有收到错误,但单元格的尺寸错误。例如,使用 layer_number =2,即我想创建两个矩阵,我得到一个维度为 (1,20,9) 的单元格,其中 20 是行数,9 是列数。我想要获得的是一个尺寸为 (2,..) 的单元格。

标签: python loops matrix cell


【解决方案1】:

我用这段代码解决了我的问题。

W = []
cells = []
for i in range(0,layer_number):
    row_W = int(input('NumRow = '))
    col_W = int(input('Numcols = '))
    W = 0.5*np.random.rand(row_W,col_W)
    cells.append([W])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多