1. Transpose Matrix
1. Get the number of rows and columns of the matrix.
2. For getting Matrix 1
i. Create an empty list a for the first matrix.
ii. Prompt the user to enter the elements of matrix 1.
iii. Append each element to the list a.
3. Initialize Result Matrix:
i. Create an empty list re for the result matrix.
ii. Initialize this matrix with zeros.
4. Perform Matrix Transposition:
i. Iterate through each element of the matrix.
ii. Transpose the elements by swapping indices: re[i][j] = a[j][i].
5. Print the resultant matrix re.
21. Transpose of a matrix
print("Matrix Transpose")
r,c = map(int, input("enter the row and column of matrix-->").split())
a=[]
print("Enter",r*c," elements of matrix1")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
a.append(a1)
re=[]
for i in range(0,r):
a1=[]
for j in range(0,c):
a1.append(0)
re.append(a1)
for i in range(0,r):
for j in range(0,c):
re[i][j]=a[j][i]
print ("The resultant matrix is")
print (re)
Output:
Matrix Transpose
enter the row and
column of matrix-->2
2
Enter 4 elements of
matrix1
1
2
3
4
The resultant matrix is
[[1, 3], [2, 4]]
Matrix Addition:
1. Get the number of rows and columns of the matrices.
2. For getting Matrix 1
i. Create an empty list a for the first matrix.
ii. Prompt the user to enter the elements of matrix 1.
iii. Append each element to the list a.
3. For getting Matrix 2
i. Create an empty list b for the second matrix.
ii. Prompt the user to enter the elements of matrix 2.
iii. Append each element to the list b.
4. Initialize Result Matrix:
i. Create an empty list re for the result matrix.
ii. Initialize this matrix with zeros.
5. Perform Matrix Addition:
2. i. Iterate through each element of the matrices.
ii. Add the corresponding elements of matrix 1 and matrix 2.
iii. Store the result in the corresponding position in the result matrix re.
6. Print the resultant matrix re.
Addition of matrix
print("Matrix Addition")
r,c = map(int, input("enter the row and column of matrix-->").split())
a=[]
print("Enter",r*c," elements of matrix1")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
a.append(a1)
b=[]
print("Enter",r*c," elements of matrix2")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
b.append(a1)
re=[]
for i in range(0,r):
a1=[]
for j in range(0,c):
a1.append(0)
re.append(a1)
for i in range(0,r):
for j in range(0,c):
re[i][j]=a[i][j]+b[i][j]
print ("The resultant matrix is")
print (re)
Output
Matrix Addition
enter the row and
column of matrix-->2
2
Enter 4 elements of
matrix1
1
2
3
4
Enter 4 elements of
matrix2
1
2
3
4
The resultant matrix is
[[2, 4], [6, 8]]
Matrix Subtraction:
1. Get the number of rows and columns of the matrices.
2. For getting Matrix 1
i. Create an empty list a for the first matrix.
ii. Prompt the user to enter the elements of matrix 1.
iii. Append each element to the list a.
3. For getting Matrix 2
i. Create an empty list b for the second matrix.
ii. Prompt the user to enter the elements of matrix 2.
iii. Append each element to the list b.
4. Initialize Result Matrix:
i. Create an empty list re for the result matrix.
ii. Initialize this matrix with zeros.
5. Perform Subtraction Addition:
i. Iterate through each element of the matrices.
3. ii. Subtract the corresponding elements of matrix 1 and matrix 2.
iii. Store the result in the corresponding position in the result matrix re.
6. Print the resultant matrix re.
Subtraction of matrix
print("Matrix Subtraction")
r,c = map(int, input("enter the row and column of matrix-->").split())
a=[]
print("Enter",r*c," elements of matrix1")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
a.append(a1)
b=[]
print("Enter",r*c," elements of matrix2")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
b.append(a1)
re=[]
for i in range(0,r):
a1=[]
for j in range(0,c):
a1.append(0)
re.append(a1)
for i in range(0,r):
for j in range(0,c):
re[i][j]=a[i][j]+b[i][j]
print ("The resultant matrix is")
print (re)
Output
Matrix Subtraction
enter the row and
column of matrix-->2
2
Enter 4 elements of
matrix1
1
2
3
4
Enter 4 elements of
matrix2
4
3
2
1
The resultant matrix is
[[-3, -1], [1, 3]]
Matrix Multiplication:
1. Get the number of rows and columns of the matrices.
2. For getting Matrix 1
i. Create an empty list a for the first matrix.
ii. Prompt the user to enter the elements of matrix 1.
iii. Append each element to the list a.
3. For getting Matrix 2
i. Create an empty list b for the second matrix.
ii. Prompt the user to enter the elements of matrix 2.
iii. Append each element to the list b.
4. Initialize Result Matrix:
i. Create an empty list re for the result matrix.
ii. Initialize this matrix with zeros.
5. Perform Matrix Multiplication:
i. Iterate through each element of the matrices.
4. ii. For each element in re[i][j], calculate the sum of the products of the corresponding
elements in matrix 1 and matrix 2.
iii. Store the result in the corresponding position in the result matrix re.
6. Print the resultant matrix re.
Multiplication of matrix
print("Matrix Multiplication")
r,c = map(int, input("enter the row and column of matrix-->").split(" "))
a=[]
print("Enter",r*c," elements of matrix1")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
a.append(a1)
b=[]
print("Enter",r*c," elements of matrix2")
for i in range(0,r):
a1=[]
for j in range(0,c):
e=int(input())
a1.append(e)
b.append(a1)
re=[]
for i in range(0,r):
a1=[]
for j in range(0,c):
a1.append(0)
re.append(a1)
for i in range(0,r):
for j in range(0,c):
for k in range(0,c):
re[i][j]+=a[i][k]*b[k][j]
print ("The resultant matrix is")
print (re)
Output
Matrix Multiplication
enter the row and
column of matrix-->2
2
Enter 4 elements of
matrix1
1
2
3
4
Enter 4 elements of
matrix2
4
3
2
1
The resultant matrix is