In this post, you will learn how to write a python program to add two matrices by taking user input with a very simple explanation and algorithm.

Rule to add two Matrices

You can add two matrices if the number of rows and number of columns are the same for both the matrix.

Python Program to Add Two Matrices

NOTE: Matrix means 2D-List in python.

Example:

matrix = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
]

Algorithm for Addition of two Matrices

1.Ask the user for the input of rows (m) & columns (n) of the matrix.
2.Using input()function and list comprehension create matrix_A and
matrix_B of dimension m x n.
3.For storing the result, create another matrix (result) of the same
dimension m x n; initially, it is Zero.
4.Using nested for-loop do result[i][j] = matrix_A[i][j] + matrix_B[i][j] .
5.At last print resultant matrix (print(result)).

From the above algorithm, we understood how to implement the python program to add two matrices by taking input from the user.

But before writing a program few programming concepts you have to know:

  1. List comprehension
  2. for-loop &
  3. How to take user input in python.

Source code

# matrix addition in python

rows = int(input('ENter number of rows: '))
cols  = int(input('ENter number of column: '))

print() # for new line
print('Enter values for matrix A')

matrix_A = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(cols)] for i in range(rows) ]  

print() # for new line
print('Enter values for matrix B ')

matrix_B = [[int(input(f"column {j+1} -> ENter {i+1} element:")) for j in range(cols)] for i in range(rows) ]  

print() #for new line

print('Matrix-A :')
for i in matrix_A:
    print(i)

print() #for new line

print('Matrix-B :')
for i in matrix_B:
    print(i)

# resultant matrix (matrix that store answer and intially it is Zero)
result = [[0 for j in range(cols)] for i in range(rows)]

# addition
for i in range(rows):
    for j in range(cols):
        result[i][j] = matrix_A[i][j] + matrix_B[i][j]

print() #for new line
print('Addition of Matrix-A and Matrix-B is :')

for i in result:
    print(i)

Output-1

ENter number of rows: 2
ENter number of column: 2

Enter values for matrix A
column 1 -> ENter 1 element:1
column 2 -> ENter 1 element:2
column 1 -> ENter 2 element:3
column 2 -> ENter 2 element:4

Enter values for matrix B
column 1 -> ENter 1 element:2
column 2 -> ENter 1 element:3
column 1 -> ENter 2 element:1
column 2 -> ENter 2 element:2

Matrix A
[1, 2]
[3, 4]

Matrix B
[2, 3]
[1, 2]

Addition of Matrix-A and Matrix-B is :
[3, 5]
[4, 6]

Output-2

ENter number of rows: 3
ENter number of column: 3

Enter values for matrix A
column 1 -> ENter 1 element:1
column 2 -> ENter 1 element:2
column 3 -> ENter 1 element:3
column 1 -> ENter 2 element:4
column 2 -> ENter 2 element:5
column 3 -> ENter 2 element:6
column 1 -> ENter 3 element:7
column 2 -> ENter 3 element:8
column 3 -> ENter 3 element:9

Enter values for matrix B
column 1 -> ENter 1 element:5
column 2 -> ENter 1 element:6
column 3 -> ENter 1 element:7
column 1 -> ENter 2 element:1
column 2 -> ENter 2 element:2
column 3 -> ENter 2 element:3 
column 1 -> ENter 3 element:4
column 2 -> ENter 3 element:9
column 3 -> ENter 3 element:7

Matrix-A :
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Matrix-B :
[5, 6, 7]
[1, 2, 3]
[4, 9, 7]

Addition of Matrix-A and Matrix-B is :
[6, 8, 10]
[5, 7, 9]
[11, 17, 16]

Related program:

Subtraction of Two Matrix in python with User input

Matrix multiplication in Python with user input

Transpose of a Matrix in Python with user input

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment