Click4Ai

23.

Easy

Write a function matrix_add(A, B) that adds two matrices element-wise without using numpy's + operator on arrays. Both matrices have the same dimensions.

Example:

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]

Output: [[6, 8], [10, 12]]

Constraints:

  • Both matrices have the same shape
  • Dimensions: 1 <= rows, cols <= 100
  • Return a list of lists
  • Test Cases

    Test Case 1
    Input: [[1, 2], [3, 4]], [[5, 6], [7, 8]]
    Expected: [[6, 8], [10, 12]]
    Test Case 2
    Input: [[0, 0], [0, 0]], [[1, 1], [1, 1]]
    Expected: [[1, 1], [1, 1]]
    Test Case 3
    Input: [[10]], [[20]]
    Expected: [[30]]
    + 2 hidden test cases