All problems
Matrix Multiplication
Problem
Multiply two matrices A (m×n) and B (n×p) to produce C (m×p).
C[i][j] = sum(A[i][k] * B[k][j] for k in range(n)).
You may assume the inner dimensions match (A's column count equals B's row count).
Hint
Triple nested loop: i over rows of A, j over cols of B, k over the shared dimension.
Test cases
case 1
in: [[[1,2],[3,4]],[[5,6],[7,8]]]
expected: [[19,22],[43,50]]
case 2
in: [[[1,0],[0,1]],[[1,2],[3,4]]]
expected: [[1,2],[3,4]]
case 3
in: [[[1,2,3]],[[4],[5],[6]]]
expected: [[32]]
case 4
in: [[[2,0],[0,3]],[[1,0],[0,1]]]
expected: [[2,0],[0,3]]
Starter codePython
def matrix_multiply(A, B):
# Your code here
pass4 test cases · run your code against them in the editor