如何总结矩阵矩阵

时间:2017-11-15 15:39:19

标签: python-2.7

我有一个矩阵,其中每个点都是另一个矩阵,我需要打印一个新矩阵,它是同一索引中每个矩阵之和的一种形式。

 >>sum_matrices([[[1,2,3],[4,5,6]],[[11,12,13],[14,15,16]],[[21,22,23],[24,25,26]],[[31,3 2,13],[34,35,36]]]) 

    [[64, 68, 52], [76, 80, 84]]

我有这个代码,它将2个矩阵加在一起。

def sum_matrices(mat_lst):
    result = []
    for i in range(len(mat_lst)):
        rows=[]
        for j in range(len(mat_lst)):
            rows.append(add_matrices(mat_lst[i][j]))
        result.append(rows)
    return result

1 个答案:

答案 0 :(得分:0)

尝试这样,




  def sum(矩阵):
 result = matrix [0] [:]
对于范围内的i(1,len(矩阵)):
对于范围内的j(len(结果)):
对于范围内的k(len(result [j])):
结果[j] [k] + =矩阵[i] [j] [k]

打印结果
  


相关问题