Python Program

Object:- Python Program to Transpose a Matrix

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Program to transpose a matrix using nested loop

X = [[6,7],
    [4 ,5],
    [3 ,2]]

result = [[0,0,0],
         [0,0,0]]

# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
       result[j][i] = X[i][j]

for r in result:
   print(r)

Output

[6, 4, 3]
[7, 5, 2]

Explanation

Transpose of a matrix is the interchanging of rows and columns. It is denoted as X'. The element at ith row and jth column in X will be placed at jth row and ith column in X'. So if X is a 3x2 matrix, X' will be a 2x3 matrix.