# Matrix Addition Program in Python

Matrix Addition Program in Python | Here, we will discuss the matrix addition program in Python. For adding any two matrices, both of the matrices should be of the same dimension. We carry out the addition by adding corresponding elements together. Similar to the add operation, we can also implement other mathematical operations. Also See:- How to Create a Matrix in Python

We will see these below Python program examples:

1. Matrix Addition In Python Using NumPy
2. Matrix Addition In Python User Input
3. Matrix Addition In Python Using Function
4. Matrix Addition In Python Using Nested Loop
5. Matrix Subtraction In Python

## Python Program to Add Two Matrices Using NumPy

NumPy is a library in python which has several functions that make it easy for the programmer to concentrate on the logic part. In this section, we use NumPy for the addition of two matrices in python.

In the below code, we have imported NumPy as np. After that, we have declared matrix 1 and matrix 2 as m1 and m2 respectively and they are added by using numpy.add() function. The resultant matrix is stored to the result variable.

Program Description:- Write a Python Program to Add Two Matrices.

``````import numpy as np
m1 = np.array([[3, 4], [5, 6]])
m2 = np.array([[8, 9], [0, 6]])
print(result)``````

Output:-

[ [11,13]
[5,12] ]

By adding m1 [3,4] and [5,6] and m2 [8,9] and [0,6] we get [11,13] and [5,12].

## Matrix Addition in Python User Input

Here we will add matrices by taking user input. We will use a list and map. The lists are used to store multiple items into a single variable, it is one of the data types in Python. A map is an inbuilt function that allows us to process and transform all the items in an iterable without using a loop.

``````# Python program to add two matrix user input

# take first matrix inputs
m1 = [list(map(int, input("Enter row: ").split(" ")))
for i in range(int(input("Enter Number of rows: ")))]

# take second matrix inputs
m2 = [list(map(int, input("Enter row: ").split(" ")))
for i in range(int(input("Enter Number of rows:")))]

r = [[m1[i][j] + m2[i][j]
for j in range(len(m1))] for i in range(len(m1))]
print(r)``````

Output:-

Enter Number of rows: 2
Enter the row: 2 1
Enter the row: 1 4
Enter Number of rows: 2
Enter the row: 6 5
Enter the row: 5 5
[[8,6] [6,9]]

In the above code, we first take the matrix from the user and store it in m1, then similarly we take m2 and we read all the row elements as well. Then we add both the matrix and put it in the result. While adding, we use a for loop to check whether the dimension is the same or not.

In the output shown first it takes the number of rows for matrix 1 and reads the matrix elements similarly it is done for matrix 2 then it adds and gives the result.

• Elements of matrix 1 are [2,1] and [1,4]
• Elements of matrix 2 are [6,5] and [5,5]
• By adding these two we get [8,6] and [6,9]

## Matrix Addition in Python Using Function

In python we can define our own function, so by using this we can add two matrices. Here in the below code, we have defined a function called add() which takes two parameters m1 and m2.

``````def add(m1, m2):
new = []
for i in range(0, len(m1)):
new.append(m1[i] + m2[i])
return new

# main
m1 = [[9, 8], [9, 0]]
m2 = [[8, 8], [6, 5]]
c = []
for i in range(0, len(m1)):

print("Resultant Matrix:")
print(c)``````

Output:

Resultant Matrix:
[[17,16], [15,5]]

In the add() function, we initiate new to an empty array, then we append new to the addition of matrix 1 and matrix2. Then in the main function, we take two matrices and then call add() to add the matrices and store them in the variable “c”.

By adding matrix 1 [9,8] and [9,0] and matrix2 [8,8] and [6,5] we get the above output that is [17,16] [15,5].

## Matrix Addition Program in Python Using a Nested Loop

A nested loop is a loop inside another loop. In this example, we use nested for-loops to add matrices.

Program Description:- Python Program to Add Two Matrices Taking Input From the User

``````# Python program to add two matrices

r = int(input("Enter the rows: "))
c = int(input("Enter the columns: "))

print("Enter Matrix 1:")
m1 = [[int(input()) for i in range(c)] for i in range(r)]
print("Matrix 1 is: ")
for n in m1:
print(n)

print("Enter Matrix 2:")
m2 = [[int(input()) for i in range(c)] for i in range(r)]
for n in m2:
print(n)

r = [[0 for i in range(c)] for i in range(r)]
for i in range(len(r)):
for j in range(c):
r[i][j] = [m1[i][j] + m2[i][j]]
for i in r:
print(i)``````

Output:-

Enter the rows: 2
Enter the columns: 2
Enter Matrix 1:
1
2
3
4
Matrix 1 is:
[1, 2]
[3, 4]
Enter Matrix 2:
5
6
7
8
[5, 6]
[7, 8]
[, ]
[, ]

In the above code, we have used a nested for loop to add m1 and m2. The outer loop reads the row elements the inner loop reads the column elements and stores the result in r.

## Matrix Subtraction in Python

Here, we will subtract two matrices by using NumPy. Subtracting two matrices is as simple as performing addition and NumPy has been made easier by numpy.subtract().

``````import numpy as np
m1 = np.array([[3, 4], [5, 6]])
m2 = np.array([[8, 9], [0, 6]])
r = np.subtract(m2, m1)
print(r)``````

Output:-

[[5,5]
[-5, 0]]

This python program also performs the same task but in different ways. In the previous program, we used NumPy to subtract two matrices but in this program, we use subtracting two matrices without using NumPy.

``````# Python program to subtract two matrix

# Take first matrix inputs
m1 = [list(map(int, input("Enter row: ").split(" ")))
for i in range(int(input("Enter Number of rows: ")))]

# Take second matrix inputs
m2 = [list(map(int, input("Enter row: ").split(" ")))
for i in range(int(input("Enter Number of rows:")))]

# subtract these matrix
print("Subtraction of Matrix:")
r = [[m1[i][j] - m2[i][j]
for j in range(len(m1))] for i in range(len(m1))]
print(r)``````

Output:

Enter Number of rows: 2
Enter row: 10 10
Enter row: 11 12
Enter Number of rows: 2
Enter row: 6 5
Enter row: 5 5
Subtraction of Matrix:
[[4,5] [6,7]]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!