【问题标题】:python convert list of matrices in a single matrixpython在单个矩阵中转换矩阵列表
【发布时间】:2020-04-14 10:26:31
【问题描述】:

从一个 for 循环中,我获得了一个矩阵列表。每个矩阵作为一个维度 [m*n]。我将 spettri_cp1 称为矩阵列表。目前我有 12 个矩阵。我想构建一个维度为 [m,12*n] 的单个矩阵。我尝试了这个循环,但我再次获得了一个新列表。 列表的维度 spettro_cp1 = [12,224,18]

mat_cp1=[]  #i define the new matrix
for i in range(12):
    for j in range(18):
        frame = spettri_cp1[i,:,:] # from the list i extract the first matrix
        spettro = frame[:,j] # from the first matrix i extract the single column
mat_cp1.append(spettro) # i append all the extracted column to build the new matrix

怎么了?

【问题讨论】:

标签: python arrays numpy sorting


【解决方案1】:
import random

m=3
n=4
listOfMatrices = [ [[random.random() for k in range(m)] for p in range(n)] for i in range(12)] #first create a list of random 12 matrix with m=3, n=4
flatList = [i for j in listOfMatrices for i in j]
flatList  = list(map(list, zip(*flatList ))) # transpose to get the desired shape

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 2021-11-25
    • 1970-01-01
    • 2019-04-23
    • 2020-09-07
    相关资源
    最近更新 更多