0% found this document useful (0 votes)
11 views10 pages

Matrix

Uploaded by

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

Matrix

Uploaded by

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

Matrix

Write a program to read the elements from a matrix

a = [1,2,3,4]
b = [5,6,7,8]
c = [9,10,11,12]

mat = [a,b,c]

for i in range(len(mat)):
for j in range(len(mat[i])):
print(mat[i][j])

Write a program to read user entered matrix values.

row = int(input("Enter number of rows:"))


column = int(input("Enter number of columns:"))

mat=[]

for i in range(row):
row = []
print("Enter {} row elements:".format(i+1))
for j in range(column):
row.append(int(input()))
mat.append(row)

print(mat)

Define a method to return the sum of matrix elements

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]

Matrix 1
]

sum = 0

for row in matrix:


for element in row:
sum = sum+element

print("The sum of all elements in the matrix is:", sum)

Define a method to return biggest element from the matrix

def max_element_without_builtins(matrix):
max_element = matrix[0][0]

for row in matrix:


for element in row:
if element > max_element:
max_element = element

return max_element

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

print("The biggest element in the matrix is:", max_element_wi

Define a method to return even count and odd count from the matrix

def count_even_odd(matrix):
even_count = 0
odd_count = 0

for row in matrix:

Matrix 2
for element in row:
if element % 2 == 0:
even_count += 1
else:
odd_count += 1

return even_count, odd_count

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

even_count, odd_count = count_even_odd(matrix)


print("Even count:", even_count)
print("Odd count:", odd_count)

Define a method to return prime number count of the matrix.

def prime_count(matrix):
prime_count = 0

for row in matrix:


for element in row:
if is_prime(element):
prime_count += 1

return prime_count

matrix = [
[2, 3, 4],
[5, 6, 7],
[8, 9, 10]
]

print("Prime count:", prime_count(matrix))

Matrix 3
Define a method how many happy number count in matrix.

Define a method to return the rowwise sum of matrix elements

def rowSum(mat):

sumList=[]

for i in mat:
sum = 0
for j in i:
sum = sum+j
sumList.append(sum)

return sumList

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)

sum = rowSum(a)
for i in sum:
print(i)

Define a method to return columnwise sum of matrix

def columnSum(mat):
sumList=[]
for column in range(len(mat[0])):
sum = 0
for row in mat:
sum = sum+row[column]
sumList.append(sum)
return sumList

a = [[1,2,3],[4,5,6],[7,8,9]]

Matrix 4
for i in a:
print(i)

sum = columnSum(a)
for i in sum:
print(i)

Define a method to return diagonalwise sum

def diagonalwiseSum(mat):
sumList=[0,0] #primary and secondary diagonal
f = 0
l = len(mat)-1
while f<len(mat) and l>=0:
sumList[0] = sumList[0] + mat[f][f]
sumList[1] = sumList[1] + mat[f][l]
f = f+1
l = l-1
return sumList

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)

print("----------")
print(diagonalwiseSum(a))

Define a method to reverse rowwise

def rowwiseReverse(mat):

for i in range(len(mat)):
f = 0
l = len(mat[i])-1
while f<l:
mat[i][f],mat[i][l] = mat[i][l],mat[i][f]

Matrix 5
f = f+1
l = l-1

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)

rowwiseReverse(a)
print("-----------")
for i in a:
print(i)

Define a method to transpose the matrix

mat = 2 8 2 5 7
5 6 => 8 6 9
7 9

def transpose(mat):
newMat = []
for i in range(len(mat[0])):
row = []
for j in range(len(mat)):
row.append(mat[j][i])
newMat.append(row)
return newMat

a = [[1,2,3],[4,5,6]]

for i in a:
print(i)

print("----------")

for i in transpose(a):
print(i)

Matrix 6
#Without creating new matrix
def transpose(mat):
n = len(mat)
for i in range(n):
for j in range(i+1,n):
mat[i][j],mat[j][i] = mat[j][i],mat[i][j]

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)

print("----------")
transpose(a)
for i in a:
print(i)

Define a method to rotate 90° left

def rotate90Left(mat):
n = len(mat)
for i in range(n):
for j in range(i+1,n):
mat[i][j],mat[j][i] = mat[j][i],mat[i][j]

f = 0
l = len(mat[i])-1
while f<l:
mat[f][i],mat[l][i] = mat[l][i],mat[f][i]
f=f+1
l=l-1

a = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)

Matrix 7
print("----------")
rotate90Left(a)
for i in a:
print(i)

Define a method to add two matrices and return resultant matrix

def addMatrix(mat1,mat2):
if len(mat1) != len(mat2) or len(mat1[0])!=len(mat2[0]):
return None

result=[]

for i in range(len(mat1)):
row=[]
for j in range(len(mat1[i])):
row.append(mat1[i][j]+mat2[i][j])
result.append(row)

return result

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[4,5,6],[7,8,9]]

c = addMatrix(a, b)

try:
for i in c:
print(i)
except:
print("Given matrices are not of same size")

Define a method to return determinant of 3x3 matrix.

Define a method to multiply 2 matrix

Matrix 8
def multiply(mat1,mat2):
if len(mat1[0]) != len(mat2):
return None

result = []
for i in range(len(mat1)):
row = []
for j in range(len(mat2[0])):
row.append(0)
result.append(row)

for i in range(len(mat1)):
for j in range(len(mat2[0])):
for k in range(len(mat1[0])):
result[i][j] += mat1[i][k] * mat2[k][j]
return result

a = [[1,2,3],[4,5,6],[7,8,9]]
b = [[1,2,3],[4,5,6],[7,8,9]]

for i in a:
print(i)
print("-------")
for i in b:
print(i)

c = multiply(a, b)
print("==========")
for i in c:
print(i)

Display Matrix elements in spiral order → Clockwise.

def spiralClockwise(mat):
n = len(mat)
i,j = 0,n-1
while i<j:
for k in range(i,j): #1st row

Matrix 9
print(mat[i][k],end=" ")
for k in range(i,j): #last column
print(mat[k][j],end=" ")
for k in range(j,i,-1): #last row
print(mat[j][k],end=" ")
for k in range(j,i,-1): #1st column
print(mat[k][i],end=" ")
i = i+1
j = j-1
if n%2==1:
print(mat[n//2][n//2],end=" ")

a = [[1,2,3],[4,5,6],[7,8,9]]
for i in a:
print(i)
print("-------------")
spiralClockwise(a)

Matrix 10

You might also like