Python Program to Transpose a Matrix

Created with Sketch.

Python Program to Transpose a Matrix

Transpose Matrix:

If you change the rows of a matrix with the column of the same matrix, it is known as transpose of a matrix. It is denoted as X’. For example: The element at ith row and jth column in X will be placed at jth row and ith column in X’.

Let’s take a matrix X, having the following elements:

  1. X = [[1,2],
  2.        [4,5],
  3.       [7,8]]

See this example:

  1. X = [[1,2],
  2.       [4,5],
  3.      [7,8]]
  4. Result = [[0,0,0],
  5.              [0,0,0]]
  6. # iterate through rows
  7. for i in range(len(X)):
  8.    for j in range(len(X[0])):
  9.        result[j][i] = X[i][j]
  10. for r in result:
  11.    print(r)

Output:

Python Nativ Data Programs4

 

Leave a Reply

Your email address will not be published. Required fields are marked *