Matrix Addition in Python User Inputs

Matrix Addition in Python User Inputs | Here, we will discuss matrix addition in python user inputs. For adding any two matrices, both 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.

Python Program to Add Two Matrix using User Inputs

Here we add matrices by taking user inputs, we use a list and map. We use lists to store multiple items of 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 inputs

# 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 :")))]

# add these matrix
print("Add Matrix:")
r = [[m1[i][j] + m2[i][j]
      for j in range(len(m1[0]))] for i in range(len(m1))]
print(r)

Output:-

Enter Number of rows: 2
Enter row: 7 6 3
Enter row: 8 9 1
Enter Number of rows: 2
Enter row: 9 3 1
Enter row: 1 8 9
Add Matrix:
[[16, 9, 4], [9, 17, 10]]

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 [7, 6, 3] and [8, 9, 1]
  • Elements of matrix 2 are [9, 3, 1] and [1, 8, 9]
  • By adding these two we get [16, 9, 4] and [9, 17, 10]

Matrix Addition in Python User Inputs

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

Program description:- Write a python program to add two matrices taking input from a user

# Python program to add two matrices user input

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]]
print("Add Matrix:")
for i in r:
   print(i)

Output:

Enter the rows: 3
Enter the columns: 3
Enter Matrix 1:
10
12
15
18
16
13
17
19
21
Matrix 1 is:
[10, 12, 15]
[18, 16, 13]
[17, 19, 21]
Enter Matrix 2:
21
25
19
26
18
13
10
20
12
[21, 25, 19]
[26, 18, 13]
[10, 20, 12]
Add Matrix:
[[31], [37], [34]]
[[44], [34], [26]]
[[27], [39], [33]]

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 User Input

In the previous program, we will add two matrices in python but in this program, we will subtract two matrices in python.

# 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[0]))] for i in range(len(m1))]
print(r)

Output:

Enter Number of rows: 3
Enter row: 45 32 56
Enter row: 12 54 89
Enter row: 36 85 45
Enter Number of rows: 3
Enter row: 1 3 5
Enter row: 0 8 7
Enter row: 5 7 3
Subtraction of Matrix:
[[44, 29, 51], [12, 46, 82], [31, 78, 42]]

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!

Leave a Comment

Your email address will not be published. Required fields are marked *