# Functions and Matrix
# Matrix Subtraction
# Function - Read Matrix
def fn_read(r, c):
M = []
print("Enter values of Matrix : ")
for i in range(0,r,1):
tmp = []
for j in range(0,c,1):
ele = int(input())
tmp.append(ele)
M.append(tmp)
return M
# Function - Display Matrix
def fn_display(M):
for row in M:
for ele in row:
print(ele, end=" ")
print()
# Function - Matrix Subtraction
def fn_sub(MA, MB, r, c):
M = []
for i in range(0,r,1):
tmp = []
for j in range(0,c,1):
ele = MA[i][j] - MB[i][j]
tmp.append(ele)
M.append(tmp)
return M
# Main Program
import sys
Mat_A = []
Mat_B = []
Mat_Sub = []
print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))
# Subtraction
if ( (r1 != r2) or (c1 != c2) ):
print("Rows and columns of given matrices do not match.")
print("Cannot Subtract the matrices.")
else:
Mat_A = fn_read(r1, c1)
Mat_B = fn_read(r2, c2)
print("Given matrix 1 : ")
fn_display(Mat_A)
print("Given matrix 2 : ")
fn_display(Mat_B)
Mat_Sub = fn_sub(Mat_A, Mat_B, r1, c1)
print("Difference of given matrices : ")
fn_display(Mat_Sub)
# Matrix Subtraction
# Function - Read Matrix
def fn_read(r, c):
M = []
print("Enter values of Matrix : ")
for i in range(0,r,1):
tmp = []
for j in range(0,c,1):
ele = int(input())
tmp.append(ele)
M.append(tmp)
return M
# Function - Display Matrix
def fn_display(M):
for row in M:
for ele in row:
print(ele, end=" ")
print()
# Function - Matrix Subtraction
def fn_sub(MA, MB, r, c):
M = []
for i in range(0,r,1):
tmp = []
for j in range(0,c,1):
ele = MA[i][j] - MB[i][j]
tmp.append(ele)
M.append(tmp)
return M
# Main Program
import sys
Mat_A = []
Mat_B = []
Mat_Sub = []
print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))
r2 = int(input("Matrix 2 - Row : "))
c2 = int(input("Matrix 2 - Column : "))
# Subtraction
if ( (r1 != r2) or (c1 != c2) ):
print("Rows and columns of given matrices do not match.")
print("Cannot Subtract the matrices.")
else:
Mat_A = fn_read(r1, c1)
Mat_B = fn_read(r2, c2)
print("Given matrix 1 : ")
fn_display(Mat_A)
print("Given matrix 2 : ")
fn_display(Mat_B)
Mat_Sub = fn_sub(Mat_A, Mat_B, r1, c1)
print("Difference of given matrices : ")
fn_display(Mat_Sub)
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu