Skip to content

Matrix Addition

Background

Matrix addition is the operation of adding two matrices by adding the corresponding entries together. Both matrices must have the same number of rows and columns.

Task

Write a function matrix_add(mat1, mat2) that computes the element-wise sum of two matrices.

Specifications

  • The function takes two 2D lists mat1 and mat2.
  • It returns a new 2D list containing the sums.
  • You may assume both matrices are always the same valid non-empty dimensions.

Constraints

  • The input matrices will have identical sizes.
  • Values can be integers or floats.

Example

>>> matrix_add([[1, 2], [3, 4]], [[5, 6], [7, 8]])
[[6, 8], [10, 12]]

Instructions

  1. Implement the logic in solution.py.
  2. Do not use input() or print().
  3. Test your solution locally before submitting.

Solve Online | Ver en Español, | Leer en Español