Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-279

Matrix Subtraction

Problem

Subtract matrix B from matrix A entrywise.

Return a new matrix where each entry is A[i][j] - B[i][j]. Both matrices have the same shape.

Hint

Subtract entrywise with a nested list comprehension.

Test cases

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

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