Tuesday, 20 September 2016

Matrix Multiplication

Write a Python function "matmult(m1,m2)" that takes as input two matrices using this row-wise representation and returns the matrix product m1*m2 using the same representation.
You may assume that the input matrices are well-formed and have compatible dimensions.




def matmult(m1,m2):
    C = [[0 for row in range(len(m1))] for col in range(len(m2[0]))]
    for i in range(len(m1)):
        for j in range(len(m2[0])):
            for k in range(len(m2)):
                C[i][j] += m1[i][k]*m2[k][j]

    return C

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This program gives rum time error ....
    matmult([[1,1]],[[3,4],[5,6]])

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete