Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-018

Matrix Addition

Problem

Add two matrices A and B of the same shape elementwise.

Return C with C[i][j] = A[i][j] + B[i][j].

Hint

Loop over each row and column index.

Test cases

case 1
in: [[[1,2],[3,4]],[[5,6],[7,8]]]
expected: [[6,8],[10,12]]
case 2
in: [[[0,0],[0,0]],[[1,-1],[-2,2]]]
expected: [[1,-1],[-2,2]]
case 3
in: [[[-1,-2],[-3,-4]],[[1,2],[3,4]]]
expected: [[0,0],[0,0]]
case 4
in: [[[1.5,2.5]],[[0.5,-1.5]]]
expected: [[2,1]]
Starter codePython
def add_matrices(A, B):
    # Your code here
    pass

4 test cases · run your code against them in the editor