矩阵乘法

pyschools Topic 6: Question 11题目:矩阵乘法

Write a function that does matrix multiplication.
   The product of a mxn matrix with a nxp matrix results in a mxp matrix.
   A mxn matrix, with m rows and n columns, can be represented using nested lists.
   Am,n = [ [x11, x12, ..., x1n], ..., [xm1, ..., xmn] ]

def MatrixProduct(a, b):
    D = []
    for i in range(len(a)):         
        C = []
        for j in range(len(b[0])):
            total = 0            
            for k in range(len(a[0])):
                total += a[i][k] * b[k][j]
            C.append(total)
        D.append(C)
    return D

print(MatrixProduct([[1,0],[0,0]], [[0,1],[1,0]]))

image

posted @ 2020-08-05 08:52  九命猫幺  阅读(314)  评论(0编辑  收藏  举报