Table of Contents

Python code to get transpose matrix of a given Matrix.

 

Python code implementation without user-defined functions & classes

Code: 
#Python code to get transpose matrix of a given Matrix

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

a_row_size = len(a)
a_column_size = len(a[0])

a_transpose_row_size = a_column_size
a_transpose_column_size = a_row_size

b = [[0,0,0], [0,0,0]]

for i in range(0, a_row_size):
   # iterate through columns
   for j in range(0, a_column_size):
       b[j][i] = a[i][j]

print("Transpose Matrix is: ", b)

 

 

Output: 
Transpose Matrix is:  [[1, 2, 4], [2, 3, 2]]

 

 

Python code implementation using function

Code: 
#Python code to get transpose matrix of a given Matrix using function

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

def transpose_matrix_func(matrix_a):
    a_row_size = len(matrix_a)
    a_column_size = len(matrix_a[0])
    b = [[0, 0, 0], [0, 0, 0]]

    for i in range(0, a_row_size):
        # iterate through columns
        for j in range(0, a_column_size):
            b[j][i] = matrix_a[i][j]
    return b



print("Transpose Matrix is: ", transpose_matrix_func(a))

 

 

Output: 
Transpose Matrix is:  [[1, 2, 4], [2, 3, 2]]

 

 

 

 

Python code implementation using Classes

Code: 
#Python code to get transpose matrix of a given Matrix using class

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

class Transpose_matrix_class(object):
    def transpose_matrix_func(self, matrix_a):
        self.matrix_a = matrix_a
        a_row_size = len(self.matrix_a)
        a_column_size = len(self.matrix_a[0])
        b = [[0, 0, 0], [0, 0, 0]]

        for i in range(0, a_row_size):
            # iterate through columns
            for j in range(0, a_column_size):
                b[j][i] = self.matrix_a[i][j]
        return b


#Create the Object
Object_1 = Transpose_matrix_class()

#Call the function using the Object
print("Transpose Matrix is: ", Object_1.transpose_matrix_func(a))

 

 

Output: 
Transpose Matrix is:  [[1, 2, 4], [2, 3, 2]]

 

 

 

 

 

 

Enjoy Python Code By Pythonbaba 🙂