简体   繁体   English

python 在单个矩阵中转换矩阵列表

[英]python convert list of matrices in a single matrix

From a for loop i obtain a list of matrices.从 for 循环中,我获得了一个矩阵列表。 Every matrix as a dimension [m*n].每个矩阵作为一个维度 [m*n]。 i called spettri_cp1 the list of matrices.我将 spettri_cp1 称为矩阵列表。 At the moment i have 12 matrix.目前我有 12 个矩阵。 I would like to build a single matrix with a dimension [m,12*n].我想构建一个维度为 [m,12*n] 的单个矩阵。 I tried with this loop but i obtain again a new list.我尝试了这个循环,但我再次获得了一个新列表。 dimension of list spettro_cp1 = [12,224,18]列表 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

what's wrong?怎么了?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM