0% found this document useful (0 votes)
23 views

Matrix

This Python program defines functions to perform matrix operations - addition, subtraction, multiplication and transpose - on two user-input matrices of the same dimensions. It prompts the user to enter the number of rows and columns, and then the elements of each matrix row by row. It defines functions to calculate and print the result of each operation on the two matrices.

Uploaded by

saniyatemp05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Matrix

This Python program defines functions to perform matrix operations - addition, subtraction, multiplication and transpose - on two user-input matrices of the same dimensions. It prompts the user to enter the number of rows and columns, and then the elements of each matrix row by row. It defines functions to calculate and print the result of each operation on the two matrices.

Uploaded by

saniyatemp05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a Python program to compute the following operation on Matrix:

1)Addition of two matrices


2)Subtraction of two matrices
3)Multiplication of two matrices
4)Transpose of two matrices
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
result = [[0,0],
[0,0]]

# Initialize matrix
X = []
Y= []

def matrix(R,C):
o=[]
for i in range(R): # A for loop for row entries
a2 =[]
for j in range(C): # A for loop for column entries
a2.append(int(input(f"Enter elements row-wise{[i+1]}
{[j+1]}: ")))
o.append(a2)
return o

X=matrix(R,C)
print("the first matrix is: ",X)
print("For the second matrix:")
Y=matrix(R,C)
print("the second matrix is: ",Y)

#Addition of two matrices:


# iterate through rows
def addi(X,Y):
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
print("The addition result is:\n")
for r in result:
print(r)
addi(X,Y)

#Subtraction of two matrices:


# iterate through rows
def sub(X,Y):
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] - Y[i][j]
print("The subtraction result is:\n")
for r in result:
print(r)
sub(X,Y)
result1=[[0,0],[0,0]]
def mult(X,Y):
for i in range(len(result1)):
for j in range(len(result1[0])):
for k in range(len(Y)):
result1[i][j]+=X[i][k]*Y[k][j]
print("Multiplication result is:")
for r in result1:
print(r)
mult(X,Y)
#Transpose
# iterate through rows
def transpose(X,Y):
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]
print("Transpose is:\n")
for r in result:
print(r)
transpose(X,Y)

Output:

Enter the number of rows:2

Enter the number of columns:2

Enter elements row-wise[1] [1]: 1

Enter elements row-wise[1] [2]: 2

Enter elements row-wise[2] [1]: 3

Enter elements row-wise[2] [2]: 4

the first matrix is: [[1, 2], [3, 4]]

For the second matrix:

Enter elements row-wise[1] [1]: 1

Enter elements row-wise[1] [2]: 1

Enter elements row-wise[2] [1]: 1

Enter elements row-wise[2] [2]: 1


the second matrix is: [[1, 1], [1, 1]]

The addition result is:

[2, 3]

[4, 5]

The subtraction result is:

[0, 1]

[2, 3]

Multiplication result is:

[3, 3]

[7, 7]

Transpose is:

[1, 3]

[2, 4]

Process finished with exit code 0

You might also like