Matrix Exercises
Q1. Write a program to find the transpose of a 3x4 matrix e.g.
rows = int(input("Input number of rows: "))
cols = int(input("Input number of columns: "))
Matrix = [[0 for col in range(cols)] for row in range(rows)]
for row in range(rows):
print("Enter values for row -> ",row)
for col in range(cols):
print("Matrix[",row,"][",col,"]-> ",end="")
Matrix[row][col]=int(input()) # input elements from keyboard
print("\n Display Matrix ")
for row in range(rows):
for col in range(cols):
print(Matrix[row][col],end="\t")
print()
print("\nTranspose of matrix")
for row in range(cols):
for col in range(rows):
print(Matrix[col][row],end="\t")
print()
Q2. Write a program to find the sum of odd numbers in a matrix.
rows =
int(input("Input number of rows: "))
cols = int(input("Input
number of columns: "))
Matrix = [[0 for col in
range(cols)] for row in range(rows)]
sum1=0
for row in range(rows):
print("Enter values for row ->
",row)
for col in range(cols):
print("Matrix[",row,"][",col,"]->
",end="")
Matrix[row][col]=int(input()) # input
elements from keyboard
if(Matrix[row][col]%2!=0):
sum1=sum1+Matrix[row][col]
print("\n Display
Matrix ")
for row in range(rows):
for col in range(cols):
print(Matrix[row][col],end="\t")
print()
print("Sum of odd
number in a matrix ->", sum1)
Q3. Write a program to find the sum of rows and columns elements in a matrix.
Answer :
rows=int(input("Enter the number of rows -> "))cols=int(input("Enter the number of columnbs -> "))mat = [[0 for i in range(cols)] for j in range(rows)]for i in range(0, rows):for j in range(0, cols):print("mat[",i,"][",j,"]= ", end=" ")mat[i][j]=int(input())print("Display Matrix")for i in range(0, rows):for j in range(0, cols):print(mat[i][j],end="\t")print()#Calculates sum of each row of given matrixfor i in range(0, rows):sumRow = 0for j in range(0, cols):sumRow = sumRow + mat[i][j]print("Sum of " + str(i+1) +" row: " + str(sumRow))#Calculates sum of each column of given matrixfor i in range(0, rows):sumCol = 0;for j in range(0, cols):sumCol = sumCol + mat[j][i]print("Sum of " + str(i+1) +" column: " + str(sumCol))
Q4. Write a program to find the sum of diagonal principal and secondary elements in a matrix.
Answer:
rows=int(input("Enter the number of rows -> "))cols=int(input("Enter the number of columnbs -> "))mat = [[0 for i in range(cols)] for j in range(rows)]principal = 0secondary = 0;for i in range(0, rows):for j in range(0, cols):print("mat[",i,"][",j,"]= ", end=" ")mat[i][j]=int(input())print("Display Matrix")for i in range(0, rows):for j in range(0, cols):print(mat[i][j],end="\t")print()for i in range(0, rows):for j in range(0, cols):# Condition for principal diagonalif (i == j):principal += mat[i][j]# Condition for secondary diagonalif ((i + j) == (rows - 1)):secondary += mat[i][j]print("Principal Diagonal:", principal)print("Secondary Diagonal:", secondary)
OR
r=int(input("Enter the number of rows of matrix "))
c=int(input("Enter the number of columns of matrix "))
#Declration of matrix
A=[[0 for x in range(c)]for x in range(r)]
#Accept value from keyboard
for i in range(r): #for rows
for j in range(c):#for columns
print("A[%d][%d]=" %(i,j), end="")
A[i][j]=int(input())
print("Display Matrix")
for i in range(r):
for j in range(c):
print(A[i][j], end="\t")#show cells values
print()
print('Sum of diagonal values')
sum=0
for i in range(r):
for j in range(c):
if(i==j):
sum=sum+A[i][j]
print("Principal Diagonal:", sum)
c=2
sum=0
for i in range(r):
sum=sum+A[i][c]
c=c-1
Q5. Write python program to sum of two matrix/nested list.

rows=int(input("Enter
the number of rows -> "))
cols=int(input("Enter
the number of columnbs -> "))
mat = [[0 for i in
range(cols)] for j in range(rows)]
mat1 = [[0 for i in
range(cols)] for j in range(rows)]
matSum = [[0 for i in
range(cols)] for j in range(rows)]
print("Input
value First matrix ")
for i in range(0,
rows):
for j in range(0, cols):
print("mat[",i,"][",j,"]= ", end="
")
mat[i][j]=int(input())
print("Input
value Second matrix ")
for i in range(0,
rows):
for j in range(0, cols):
print("mat1[",i,"][",j,"]= ", end="
")
mat1[i][j]=int(input())
print("*******Display
Matrix1*******")
for i in range(0,
rows):
for j in range(0, cols):
print(mat[i][j],end="\t")
print()
print("******Display
Matrix2********")
for i in range(0,
rows):
for j in range(0, cols):
print(mat1[i][j],end="\t")
print()
print("Sum of
Two matrix ")
for i in range(0,
rows):
for j in range(0, cols):
matSum[i][j]=mat[i][j]+mat1[i][j]
print(matSum[i][j],end="\t")
print()
Q6. Write a python program to calculate subtract of two matrix.
cols=int(input("Enter
the number of columnbs -> "))
mat = [[0 for i in
range(cols)] for j in range(rows)]
mat1 = [[0 for i in
range(cols)] for j in range(rows)]
matSum = [[0 for i in
range(cols)] for j in range(rows)]
print("Input
value First matrix ")
for i in range(0,
rows):
for j in range(0, cols):
print("mat[",i,"][",j,"]= ", end="
")
mat[i][j]=int(input())
print("Input
value Second matrix ")
for i in range(0,
rows):
for j in range(0, cols):
print("mat1[",i,"][",j,"]= ", end="
")
mat1[i][j]=int(input())
print("*******Display
Matrix1*******")
for i in range(0,
rows):
for j in range(0, cols):
print(mat[i][j],end="\t")
print()
print("******Display
Matrix2********")
for i in range(0,
rows):
for j in range(0, cols):
print(mat1[i][j],end="\t")
print()
print("Sum of
Two matrix ")
for i in range(0,
rows):
for j in range(0, cols):
matSum[i][j]=mat[i][j]-mat1[i][j]
print(matSum[i][j],end="\t")
print()
Q7. Write a python program to calculate multiplication of two matrix.
Q8.
Write a program to input any matrix and print both diagonal values of the
matrix.
Note
: The matrix must be a square matrix, i.e. 3x3, 4x4, 5x5, etc.
Answer :
r=int(input("Enter the number
of rows of matrix "))
c=int(input("Enter the number
of columns of matrix "))
#Declration of matrix
A=[[0 for x in range(c)]for x in
range(r)]
#Accept value from keyboard
for i in range(r): #for rows
for j in range(c):#for columns
print("A[%d][%d]=" %(i,j),
end="")
A[i][j]=int(input())
print("Display Matrix")
for i in range(r):
for j in range(c):
print(A[i][j], end="\t")#show
cells values
print()
print("Principal Diagonal
:-> ", end="")
for i in range(r):
for j in range(c):
if(i==j):
print(A[i][j],end="\t")
print('\nSecondary Diagonal:->
',end="\t")
c=2
for i in range(r):
print(A[i][c],end="\t")
c=c-1
No comments:
Post a Comment