Matrix PYTHON Prgms
Matrix PYTHON Prgms
………………………………………………………………………………………………………………………………………………………………………………………………………….
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
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
m[0][0] = 10
m[0][1] = 20
m[1][0] = 30
m[1][1] = 40
m[2][0] = 50
m[2][1]= 60
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
6
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
7
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
Matrix 1
102
030
405
Matrix2
102
030
402
Added Matrix
204
060
807
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]]
add(A, B, C)
#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
………………………………………………………………………………………………………………………………………………………………………………………………………….
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
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
The matrix is :
123
456
789
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()
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
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}}
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
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.
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
2345
22
1234
11 16
19 28
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
18
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
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()
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
Input:
149
351
285
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
21
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
Output:
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
123
456
789
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)
Input:
149
351
285
Output:
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
24
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
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/
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
26
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
Input:
149
351
285
Output:
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
27
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
123
456
789
9
OUTPUT
Enter the order of the matrix : 3 3
123
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
28
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
456
789
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)
Input:
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
29
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
149
351
285
Output:
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
30
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
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)
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
31
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
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
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
………………………………………………………………………………………………………………………………………………………………………………………………………….
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
34
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
100
120
250
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
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/
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
Sum of column 1 = 1 + 4 + 7 = 12
Sum of column 2 = 2 + 5 + 8 = 15
Sum of column 3 = 3 + 6 + 9 = 18
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
37
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
123
456
789
Sum of rows is 6 15 24
Sum of columns is 12 15 18
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
38
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
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/
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
40
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
123
456
………………………………………………………………………………………………………………………………………………………………………………………………………..
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
41
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
789
for i in range(0,1):
for j in range(0, n-1):
row1 = row1 + mat[i][j]
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]
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
FLAT 605, 6th FLOOR, BLOCK A, ROYAL PAVILION, OPP BIG BAZAR,
AMEERPET, HYDERABAD 500016
44
─ The Finishing School
………………………………………………………………………………………………………………………………………………………………………………………………………….
456
789
Boundary Matrix :
123
4 6
789
Sum of boundary is 40
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