Skip to content

Commit e282f87

Browse files
committed
added a transpose file
1 parent 2c15b8c commit e282f87

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

matrix/transpose_of_matrix.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
finding tanspose of the given matrix
3+
The transpose of a matrix is a new matrix formed by
4+
flipping the original matrix over its main diagonal,
5+
effectively interchanging its rows and columns
6+
suppose A=[[1,2,3],
7+
[4,5,6],
8+
[7,8,9]]
9+
is the given matix then it's transpose will be
10+
A^t=[[1,4,7],
11+
[2,5,8],
12+
[3,6,9]]
13+
Apporoach:
14+
1. Create a empty(null) matrix of the same size of the given matrix
15+
2.traverse the given matrix by using nested for loop
16+
3.copy the elements in oppposite order of the traversl
17+
i.e. trans[j][i]=matrix[i][j]
18+
"""
19+
def transpose(matrix):
20+
#create a null matrix of same dimension of given matrix
21+
trans=[[0]*len(matrix) for _ in range(len(matrix[0]))]
22+
for i in range(len(matrix)):
23+
#row wise traversal
24+
for j in range(len(matrix[0])):
25+
#column wise traversal
26+
trans[j][i]=matrix[i][j]
27+
#copying the elements in null matrix
28+
return trans
29+
#checking for main function
30+
if __name__=="__main__":
31+
#matrix(given)
32+
mat=[[1, 2, 3],[4, 5, 6]]
33+
#call the function with mat as parameter
34+
print(transpose(mat))
35+
36+
37+
38+

0 commit comments

Comments
 (0)