0% found this document useful (0 votes)
56 views46 pages

Matrix PYTHON Prgms

Uploaded by

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

Matrix PYTHON Prgms

Uploaded by

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

─ The Finishing School

………………………………………………………………………………………………………………………………………………………………………………………………………….
1 Program to find the transpose of a matrix in C, C++, Java and Python |
2 Program to add two matrices | FACE Prep
3 Program to find the saddle point coordinates in a given matrix
4 Program to perform matrix addition, matrix subtraction, matrix multiplication
5 Program to find the minimum and maximum element in each row of a matrix
6 Find the minimum and maximum element in each column of a matrix.
7 Program to find if the given matrix is upper triangular or not | FACE Prep
8 Program to find if the given matrix is lower triangular or not | FACE Prep
9 Program to find sum of elements in each row and each column of the given matrix and print the greatest of the same
10 Sum of elements in the Zigzag sequence in a given matrix
11 Program to print the sum of boundary elements of a matrix | FACE Prep

1. Program to find the transpose of a


matrix in C, C++, Java and Python
Program to find the transpose of a matrix is
discussed here. Transpose of a matrix can be
performed by exchanging the elements of row by
column and the elements of a column by row.
For example:

Input:

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

1
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

123
456
789

output:

147
258
369

Algorithm:

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

2
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Consider the matrix:

m[0][0] = 10

m[0][1] = 20

m[1][0] = 30

m[1][1] = 40

m[2][0] = 50

m[2][1]= 60

The transpose of the above matrix can be done by


this way:

r_m[0][0] = m[0][0] = 10

r_m[0][1] = m[1][0] = 30

r_m[0][2] = m[2][0] = 50
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

3
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

r_m[1][0] = m[0][1] = 20

r_m[1][1] = m[1][1] = 40

r_m[1][2] = m[2][1] = 60
Input-
Enter number of rows:
3
Enter number of columns:
3
Enter the element:
1
1
1
2
2
2
3
3
3
Input matrix:
111
222
333
Output-
Transpose matrix:
123
123
123

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

4
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Program to find the transpose of a


matrix
matrix=[]
row=int(input(“Enter number of rows: “))
column=int(input(“Enter number of columns: “))
for i in range(row):
matrix.append([])
for j in range(column):
num=int(input(“Enter the element: “))
matrix[i].append(num)
print(‘\n’)
print(“Input matrix: “)
for i in range (row):
for j in range (column):
print (matrix[i][j], end=” “)
print(‘\n’)

transpose=[]
for j in range(column):
transpose.append([])
for i in range (row):
t_num=matrix[i][j]
transpose[j].append(t_num)

print(‘\n’)
print(“Transpose matrix: “)
for i in range (row):
for j in range (column):
print (transpose[i][j], end=” “)
print(‘\n’)

https://www.faceprep.in/c/find-the-transpose-of-a-matrix/

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

5
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

2. Program to add two matrices | FACE


Prep
Program to add two matrices is discussed here. Two
matrices are obtained as input from the user.
Addition of two matrices is possible only when both
the matrices contain same number of rows and
columns.

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

6
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Algorithm to add two matrices

 Input matrix 1 and matrix 2.


 If the number of rows and number of columns of matrix 1 and
matrix 2 is equal,
 for i=1 to rows[matrix 1]
 for j=1 to columns [matrix 1]
 Input matrix 1 [i,j]
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

7
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

 Input matrix 2 [i,j]


 matrix 3 [i,j]= matrix 1 [i,j]+ matrix 2 [i,j];
 Display matrix 3 [i,j];
OUTPUT
Enter the number of rows and columns : 3 3

Input Matrix 1 elements : 1 0 2 0 3 0 4 0 5

Matrix 1
102
030
405

Input Matrix 2 elements : 1 0 2 0 3 0 4 0 2

Matrix2
102
030
402

Added Matrix
204
060
807

Program to add two matrices


# Python program to add two matrices

N=3
# Fucntion to perform addition of matrices
def add(A,B,C):

for i in range(N):
for j in range(N):
C[i][j] = A[i][j] + B[i][j]
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

8
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

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

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

C=A[:][:] # To store the result

add(A, B, C)

print(“Result matrix is”)


for i in range(N):
for j in range(N):
print(C[i][j], ” “, end=”)
print()

Program to add two matrices using


pointers

#include<stdio.h>#include<stdlib.h>
int i,j;
int** memory(int c,int r){
int **a;
a=(int **)malloc(c*sizeof(int*));
for(i=0;i<c;i++)
*(a+i)=(int *)malloc(r*sizeof(int));

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

9
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

return a;
}
int main()
{
int **a,**b,r,c;
scanf(“%d %d”,&r,&c);
a=memory(c,r);
b=memory(c,r);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf(“%d”,&b[i][j]);
}
}for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf(“%d “,a[i][j]+b[i][j]);
}printf(“n”);
}
return 0;

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

10
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Time complexity: O(n^2)


https://www.faceprep.in/c/program-to-add-two-matrices/

3. Program to find the saddle point


coordinates in a given matrix
The program to find the saddle point coordinates in a
given matrix is discussed here. A saddle point is an
element of the matrix, which is the minimum
element in its row and the maximum in its column.

For example, consider the matrix given below

123

Mat[3][3] = 4 5 6

789

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

11
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Here, 7 is the saddle point because it is the minimum


element in its row and the maximum element in its
column.

Algorithm to find the saddle point


coordinates in a given matrix

1. Input the matrix from the user.


………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

12
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

2. Use two loops, one for traversing the row and the other for
traversing the column.
3. If the current element is the minimum element in its row and
the maximum element in its column, then return its coordinates.
4. Else, continue traversing.
OUTPUT
Enter the square matrix order : 3

Input the matrix : 1 2 3 4 5 6 7 8 9

The matrix is :

123

456

789

Saddle point (3,0) : 7


The program to find the saddle point coordinates in a
given matrix is given below.
# Python program to find the saddle point coordinates in a given matrix

from array import *

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


m=3
min = 0
max = 0
pos = [[0 for x in range(2)] for y in range(2)]
print(“nThe matrix is n”)
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

13
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
for i in range(0,m):
for j in range(0,m):
print(matrix[i][j], end = ” “)
print()

# find the saddle points in the given matrix */

for i in range(0,m):
min = matrix[i][0]
for j in range(0,m):
if (min >= matrix[i][j]):
min = matrix[i][j]
pos[0][0] = i
pos[0][1] = j
j = pos[0][1];
max = matrix[0][j];
for k in range(0,m):
if (max <= matrix[k][j]):
max = matrix[i][j]
pos[1][0] = k
pos[1][1] = j
# saddle point is the minimum of a row and maximum of the column
if (min == max):
if (pos[0][0] == pos[1][0] and pos[0][1] == pos[1][1]):
print(“nSaddle point ( “, pos[0][0] , “,” , pos[0][1] , ” ) : ” , max)
Time complexity: O(n^2)
https://www.faceprep.in/c/find-the-saddle-point-coordinates-in-a-given-matrix/

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

14
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

4. Program to perform matrix addition,


matrix subtraction, matrix
multiplication
Programs to perform all the basic Matrix operations
like Matrix addition, matrix subtraction, and matrix
multiplication are discussed here.

Matrix Addition
mat1 = {{1, 2}, {3, 4}}mat2 = {{1, 2}, {3,
4}}mat1 + mat2 = {{2, 4}, {6, 8}}

Matrix Subtraction
mat1 = {{1, 2}, {3, 4}}mat2 = {{1, 2}, {3,
4}}mat1 - mat2 = {{0, 0}, {0, 0}}

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

15
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Matrix Multiplication
mat1 = {{1, 2}, {3, 4}}mat2 = {{1, 2}, {3,
4}}mat1 * mat2 = {{7, 10}, {15, 22}}

Algorithm to perform matrix addition,


matrix subtraction, matrix
multiplication
Matrix addition:

1. Input the order of the matrix.


2. Input the matrix 1 elements.
3. Input the matrix 2 elements.
4. Repeat from i = 0 to m
5. Repeat from j = 0 to n
6. mat3[i][j] = mat1[i][j] + mat2[i][j]
7. Print mat3.

Matrix subtraction:
1. Input the order of the matrix.
2. Input the matrix 1 elements.
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

16
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

3. Input the matrix 2 elements.


4. Repeat from i = 0 to m
5. Repeat from j = 0 to n
6. mat3[i][j] = mat1[i][j] - mat2[i][j]
7. Print mat3.

Matrix multiplication:
1. Input the order of the matrix1 ( m * n).
2. Input the order of matrix2 (p * q).
3. Input the matrix 1 elements.
4. Input the matrix 2 elements.
5. Repeat from i = 0 to m
6. Repeat from j = 0 to q
7. repeat from k = 0 to p
8. sum=sum+ mat1[c][k] * mat2[k][d];
9. mat3[c][d]=sum
10. Print mat3.

Here are some of the example on programs to


perform matrix addition, matrix subtraction,
matrix multiplication
OUTPUT
2 2 (order of the matrix)

1 2 3 4 (matrix 1 elements)

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

17
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
2 3 4 5 (matrix 2 elements)

3 5 (resultant matrix)

79
OUTPUT
2 2 (order of the matrix)

5 6 7 8 (matrix 1 elements)

1 2 3 4 (matrix 2 elements)

4 4 (resultant matrix)

44
OUTPUT
Enter number of rows and columns of mat1 matrix

22

Enter elements of matrix 1

2345

Enter number of rows and columns of mat2 matrix

22

Enter elements of matrix 2

1234

Product of the matrices:

11 16

19 28
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

18
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Program to perform matrix operations |


Matrix addition
# Python program to perform matrix operations ,matrix addition, matrix subtraction, matrix
multiplication – addition

mat1 = [[1, 2], [3, 4]]


mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):


for j in range(0, 2):
mat3[i][j] = mat1[i][j] + mat2[i][j]

print(“Addition of two matrices”)


for i in range(0, 2):
for j in range(0, 2):
print(mat3[i][j], end = ” “)
print()

Program to perform basic matrix


operations | Matrix Subtraction
# Python program to perform matrix operations ,matrix addition, matrix subtraction, matrix
multiplication – subtraction

mat1 = [[10, 9], [8, 6]]


mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):


for j in range(0, 2):
mat3[i][j] = mat1[i][j] – mat2[i][j]

print(“Subtraction of two matrices”)


………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

19
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
for i in range(0, 2):
for j in range(0, 2):
print(mat3[i][j], end = ” “)
print()

Program to perform basic matrix


operations | Matrix
multiplication
# Python program to multiply two matrices

mat1 = [[10, 9], [8, 6]]


mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):


for j in range(0, 2):
mat3[i][j] = mat1[i][j] – mat2[i][j]

print(“Multiplication of two matrices”)


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

for r in mat3:
print(r)
https://www.faceprep.in/c/program-to-perform-matrix-addition-matrix-subtraction-matrix-
multiplication/

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

20
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

5. Program to find the minimum and


maximum element in each row of a
matrix
Program to find the minimum and maximum element
in each row of a matrix is discussed here. Given a
matrix, the maximum an minimum elements of each
row of the matrix are displayed as output.

Maximum element in each row of the matrix

Input:

3 3 (Order of the matrix - number of rows and


columns)

149

351

285
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

21
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Output:

Algorithm to find the maximum element in


each row of a matrix

 Input the order of the matrix.


 Input the matrix elements.
 For row = 0 to n-1
 Find the maximum element in the row and insert the element in
an array.
 Print the array.
OUTPUT
Enter the order of the matrix : 3 3

Input matrix elements :

123

456
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

22
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

789

9
OUTPUT
Enter the order of the matrix : 3 3

Input matrix elements :

123

456

789

Program to find the maximum element in


each row of a matrix
# Python program to find the maximum element in each row of a matrix

def maxi_row(arr):

no_of_rows = len(arr)
no_of_column = len(arr[0])
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

23
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

for i in range(no_of_rows):

max1 = 0
for j in range(no_of_column):
if arr[i][j] > max1 :
max1 = arr[i][j]

print(max1)

mat1 = [[1,2,3],[4,5,6],[7,8,9]]
maxi_row(mat1)

Minimum element in each row of a matrix

Input:

3 3 (Order of the matrix - number of rows and


columns)

149

351

285

Output:
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

24
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Algorithm to find the minimum element in each


row of a matrix

 Input the order of the matrix.


 Input the matrix elements.
 For row = 0 to n-1
 Find the minimum element in the row and insert the element in an ar-
ray.
 Print the array.

Program to find the minimum element in


each row of a matrix
# Python program to find the minimum element in each row of a matrix

def mini_row(arr):

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

25
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
no_of_rows = len(arr)
no_of_column = len(arr[0])

for i in range(no_of_rows):

min1 = 99999
for j in range(no_of_column):
if arr[i][j] < min1 :
min1 = arr[i][j]

print(min1)

mat1 = [[1,2,3],[4,5,6],[7,8,9]]
mini_row(mat1)
https://www.faceprep.in/c/program-to-find-the-minimum-and-maximum-element-in-each-row-
of-matrix/

6. Find the minimum and maximum


element in each column of a matrix.
Program to find the minimum and maximum element
in each column of a matrix is discussed here. Given a
matrix, the maximum an minimum elements of each
column of the matrix are displayed as output.

Maximum element in each column of the


matrix
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

26
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Input:

3 3 (Order of the matrix - number of rows and


columns)

149

351

285

Output:

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

27
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Algorithm to find the maximum element in


each column of a matrix

 Input the order of the matrix.


 Input the matrix elements.
 For row = 0 to n-1
 Find the maximum element in the column and print it.
OUTPUT
Enter the order of the matrix : 3 3

Input matrix elements :

123

456

789

9
OUTPUT
Enter the order of the matrix : 3 3

Input matrix elements :

123

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

28
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
456

789

Program to find the maximum


element in each column of a
matrix
# Python program to find the maximum element in each column of the matrix

def maxi_col(mat):
for i in range(0,n):
maxi = mat[0][i]
for j in range(1,n):
if(mat[j][i] > maxi):
maxi = mat[j][i]
print(maxi)

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


maxi_col(mat1)

Minimum element in each column of a matrix

Input:

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

29
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

3 3 (Order of the matrix - number of rows and


columns)

149

351

285

Output:

Algorithm to find the minimum element in each


column of a matrix
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

30
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

 Input the order of the matrix.


 Input the matrix elements.
 For row = 0 to n-1
 Find the minimum element in the column and print it.

Program to find the minimum element in


each column of a matrix
# Python program to find the minimum element in each column of a matrix

def mini_col(mat):
for i in range(0,n):
mini = mat[0][i]
for j in range(1,n):
if(mat[j][i] < mini):
mini = mat[j][i]
print(mini)

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


mini_col(mat1)
https://www.faceprep.in/c/find-the-minimum-and-maximum-element-in-each-column-of-a-
matrix/

7. Program to find if the given matrix is


upper triangular or not | FACE Prep
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

31
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Program to find if the given matrix is upper


triangular or not is discussed here. A square matrix is
obtained as input from the user and it is checked if it
is upper triangular or not. A square matrix is said to
be an upper triangular matrix if all the entries below
the diagonal are zero.

Algorithm to check if the given matrix is


upper triangular or not

 Input the order of the square matrix.


 Input the matrix elements.
 Repeat from i = 1 to n.
 Repeat from j = 0 to i.
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

32
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

 if (mat[i][j] != 0)
 Set flag = 0 and print "Not An Upper Triangular Matrix".
 Else, print "Upper Triangular Matrix".
OUTPUT
Input the matrix elements :

123

012

003

Upper triangular matrix

Program to check if the given matrix is


upper triangular or not
# Python program to check if the given matrix is upper triangular or not

mat = [[1, 2, 3], [4, 0, 6], [0, 0, 9]]


flag = 0
for i in range(1, n):
for j in range(0, i):
if (mat[i][j] != 0):
flag = 0
else:
flag = 1

if (flag == 1):
print(“Upper Triangular Matrix”)
else:
print(“Not an Upper Triangular Matrix”)
https://www.faceprep.in/c/find-if-the-given-matrix-is-upper-triangular-or-not/

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

33
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

8. Program to find if the given matrix is


lower triangular or not | FACE Prep
Program to find if the given matrix is lower triangular
or not is discussed here. A square matrix is obtained
as input from the user and it is checked if it is lower
triangular or not.

A square matrix is said to be a lower triangular


matrix if all the entries above the diagonal are zero.

Algorithm to check if the given matrix is


lower triangular or not
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

34
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

 Input the order of the square matrix.


 Input the matrix elements.
 Repeat from i = 0 to n.
 Repeat from j = 1+1 to n.
 if (mat[i][j] != 0)
 Set flag = 0 and print "Not a Lower Triangular Matrix".
 Else, print "Lower Triangular Matrix".
OUTPUT
Enter the order of the matrix : 3

Input the matrix elements :

100

120

250

Lower Triangular Matrix

Program to check if the given matrix is


lower triangular or not
# Python program to check if the given matrix is Lower triangular or not

def islowertriangular(M):
for i in range(0, len(M)):
for j in range(i + 1, len(M)):
if(M[i][j] != 0):
return False
return True

mat = [[1, 2, 3], [0, 5, 0], [0, 0, 0]]


if islowertriangular(mat):
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

35
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
print (“Lower Triangular Matrix”)
else:
print (“Not a Lower Triangular Matrix”)
https://www.faceprep.in/c/find-if-the-given-matrix-is-lower-triangular-or-not/

9. Program to find sum of elements in


each row and each column of the
given matrix and print the greatest
of the same
Program to find the sum of elements in each row and
each column of the given matrix and print the
greatest of the same is discussed here. Input the
matrix and find the sum of each row and each
column and display the row having the greatest sum
and also the column having the greatest sum.

For example, consider the matrix

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Sum of row 1 = 1 + 2 + 3 = 6
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

36
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Sum of row 2 = 4 + 5 + 6 = 15

Sum of row 3 = 7 + 8 + 9 = 24

Row 3 is having the greatest sum.

Sum of column 1 = 1 + 4 + 7 = 12

Sum of column 2 = 2 + 5 + 8 = 15

Sum of column 3 = 3 + 6 + 9 = 18

Column 3 is having the greatest sum.

Algorithm to find the sum of elements in


each row and each column of the given
matrix

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

37
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

 Input the number of rows and columns of the matrix.


 Input the matrix elements.
 Traverse the matrix and find the sum of each row and each col-
umn and display them.
 Find the row having the greatest sum and display it.
 Find the column having the greatest sum and display it.
OUTPUT
Enter the order of the matrix : 3 3

Input the matrix elements :

123

456

789

Sum of rows is 6 15 24

Row 3 has maximum sum

Sum of columns is 12 15 18

Column 3 has maximum sum

Program to find the sum of


elements in each row and each
column of the given matrix and
print the greatest of the same
# Python program to find thesum of elements in each row and each column of the given matrix
and print the greatest of the same
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

38
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

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


sum = 0
row_ind = 0
col_ind = 0;
m=3
n=3
row_arr = []
z=0
print(“Sum of rows is “, end = “”)
for row in range(0, m):
sum = 0
for col in range(0, n):
sum = sum + mat[row][col]
print(sum, end = ” “)
z=z+1
row_arr.append(sum)

temp_row = row_arr[0]
for i in range(1, m):
if(temp_row < row_arr[i]):
temp_row = row_arr[i]
row_ind = i
print(“Row”, row_ind+1,”has maximum sum”)
print(“Sum of columns is “, end = “”)

sum = 0
y=0
col_arr = []
for i in range(0, n):
sum = 0
for j in range(0, m):
sum = sum + mat[j][i]
print(sum, end = ” “)
y=y+1
col_arr.append(sum)

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

39
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
temp_col = col_arr[0]
for i in range(1, n):
if(temp_col < col_arr[i]):
temp_col = col_arr[i]
col_ind = i
print(“Column”, col_ind+1,”has maximum sum”)
https://www.faceprep.in/c/sum-of-elements-in-each-row-and-each-column-of-matrix-and-
print-the-greatest/

10. Sum of elements in the Zigzag sequence


in a given matrix
Program to print the sum of elements in the Zigzag
sequence in a given matrix is discussed here. Given
a matrix, the sum of elements in the zigzag
sequence is displayed as output.

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

40
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Algorithm to find the sum of elements in the


zigzag sequence in a given matrix

 Input the order of the matrix.


 Input the matrix elements.
 Find the sum of elements of the matrix in zigzag sequence.
 Print the sum.
OUTPUT
Enter the order of the matrix : 3 3

Input the matrix elements :

123

456

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

41
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
789

Sum of Zig-Zag pattern is 35

Program to find the sum of


elements in the zigzag
sequence in a given matrix
# Python Program to print the sum of elements in the Zigzag sequence in a given matrix

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


sum = 0
row1 = 0
col_n = 0
diag = 0
m=3
n=3
for i in range(0):
for j in range(0, n-1):
row1 = row1 + mat[i][j]

for i in range(0,1):
for j in range(0, n-1):
row1 = row1 + mat[i][j]

for j in range(n-1, n-2, -1):


for i in range(0, m):
col_n = col_n + mat[j][i]

for i in range(0, m):


for j in range(0, n):
if ((i + j) == (m – 1)):
diag = diag + mat[i][j]
if(j == 0 and i == m-1):
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

42
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
col_n = col_n – mat[i][j]

sum = diag + row1 + col_n;


print(“Sum of Zig-Zag pattern is “, sum)
https://www.faceprep.in/c/sum-of-elements-in-the-zigzag-sequence-in-given-matrix/

11. Program to print the sum of boundary


elements of a matrix | FACE Prep
Program to print the sum of boundary elements of a
matrix is discussed here. Given a matrix, the task is
to print the boundary elements of the matrix and
display their sum.

For example, consider the matrix given below.

123

456

789

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

43
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Boundary Matrix

123
4 6
789

Sum of boundary elements: 1 + 2 + 3 + 4 + 6 +


7 + 8 + 9 = 40

Algorithm to print the sum of boundary


elements of a matrix

 Input the order of the matrix.


 Input the matrix elements.
 Print all the boundary elements of the matrix.
 Find the sum of the boundary elements.
 Print sum.
OUTPUT
Enter the order of the matrix : 3 3
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

44
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….

Input the matrix elements :


123

456

789

Boundary Matrix :

123

4 6

789

Sum of boundary is 40

Program to print the sum of boundary


elements of a matrix
# Python Program to print the sum of boundary elements of a matrix
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = 0
m=3
n=3
print(“\nBoundary Matrix\n”)
for i in range (0, m):
for j in range (0, n):
if (i == 0 or j == 0 or i == n – 1 or j == n – 1):
print(mat[i][j], end = ” “)
sum = sum + mat[i][j]
else:
print(end = ” “)
print(“\n”)
………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

45
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
print(“\nSum of boundary is “, sum)
https://www.faceprep.in/c/print-the-sum-of-boundary-elements-of-a-matrix/

………………………………………………………………………………………………………………………………………………………………………………………………………..

FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016

46

You might also like