Python Program to Find the Transpose of a Matrix

Introduction

The transpose of a matrix is an operation that flips a matrix over its diagonal, meaning that the rows become columns and the columns become rows. In other words, the element at position (i, j) in the original matrix will be at position (j, i) in the transposed matrix. This tutorial will guide you through creating a Python program that finds the transpose of a given matrix.

Example:

  • Original Matrix:

    1 2 3
    4 5 6
    
  • Transposed Matrix:

    1 4
    2 5
    3 6
    

Problem Statement

Create a Python program that:

  • Takes a matrix as input.
  • Finds the transpose of the matrix.
  • Displays the resulting transposed matrix.

Solution Steps

  1. Take Matrix Dimensions: Ask the user for the number of rows and columns.
  2. Initialize the Matrix: Create an empty matrix to store the input values.
  3. Input Matrix Elements: Use nested loops to take input for each element of the matrix.
  4. Initialize the Transpose Matrix: Create a matrix of zeros with dimensions swapped (i.e., columns become rows and rows become columns).
  5. Calculate the Transpose: Use nested loops to swap the rows and columns of the original matrix.
  6. Display the Transposed Matrix: Print the resulting transposed matrix in a formatted manner.

Python Program

# Python Program to Find the Transpose of a Matrix
# Author: https://www.rameshfadatare.com/

# Step 1: Take the number of rows and columns as input
rows = int(input("Enter the number of rows: "))
columns = int(input("Enter the number of columns: "))

# Step 2: Initialize the matrix
matrix = []

# Step 3: Input matrix elements
print("Enter the elements of the matrix:")
for i in range(rows):
    row = []
    for j in range(columns):
        element = int(input(f"Element at position ({i+1}, {j+1}): "))
        row.append(element)
    matrix.append(row)

# Step 4: Initialize the transpose matrix
transpose_matrix = []
for i in range(columns):
    transpose_matrix.append([0] * rows)

# Step 5: Calculate the transpose of the matrix
for i in range(rows):
    for j in range(columns):
        transpose_matrix[j][i] = matrix[i][j]

# Step 6: Display the transposed matrix
print("\nThe transpose of the matrix is:")
for row in transpose_matrix:
    for element in row:
        print(element, end=" ")
    print()  # Newline after each row

Explanation

Step 1: Take the Number of Rows and Columns as Input

  • The input() function is used to take the number of rows and columns from the user. These values are converted to integers and stored in rows and columns variables.

Step 2: Initialize the Matrix

  • An empty list matrix is initialized to store the elements of the matrix.

Step 3: Input Matrix Elements

  • A nested loop is used to take input for each element of the matrix. Each element is appended to the corresponding row, and the row is then appended to matrix.

Step 4: Initialize the Transpose Matrix

  • A new matrix transpose_matrix is initialized with zeros. The dimensions of this matrix are swapped (i.e., rows becomes columns and columns becomes rows).

Step 5: Calculate the Transpose of the Matrix

  • A nested loop is used to swap the rows and columns of the original matrix. The element at position (i, j) in the original matrix is placed at position (j, i) in the transposed matrix.

Step 6: Display the Transposed Matrix

  • The transposed matrix is displayed using a nested loop, with each row printed on a new line.

Output Example

Example Output:

Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements of the matrix:
Element at position (1, 1): 1
Element at position (1, 2): 2
Element at position (1, 3): 3
Element at position (2, 1): 4
Element at position (2, 2): 5
Element at position (2, 3): 6

The transpose of the matrix is:
1 4 
2 5 
3 6 

Additional Examples

Example 1: Transposing a Square Matrix

# Example of transposing a 3x3 square matrix
rows = 3
columns = 3
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Resultant transpose matrix initialization
transpose_matrix = [[0 for _ in range(rows)] for _ in range(columns)]

# Calculating the transpose
for i in range(rows):
    for j in range(columns):
        transpose_matrix[j][i] = matrix[i][j]

# Displaying the transpose matrix
print("\nThe transpose of the square matrix is:")
for row in transpose_matrix:
    for element in row:
        print(element, end=" ")
    print()

Output:

The transpose of the square matrix is:
1 4 7 
2 5 8 
3 6 9 

Example 2: Transposing a 3×2 Matrix

# Example of transposing a 3x2 matrix
rows = 3
columns = 2
matrix = [
    [10, 20],
    [30, 40],
    [50, 60]
]

# Resultant transpose matrix initialization
transpose_matrix = [[0 for _ in range(rows)] for _ in range(columns)]

# Calculating the transpose
for i in range(rows):
    for j in range(columns):
        transpose_matrix[j][i] = matrix[i][j]

# Displaying the transpose matrix
print("\nThe transpose of the 3x2 matrix is:")
for row in transpose_matrix:
    for element in row:
        print(element, end=" ")
    print()

Output:

The transpose of the 3x2 matrix is:
10 30 50 
20 40 60 

Conclusion

This Python program demonstrates how to find the transpose of a matrix by taking input from the user for each element. The program then swaps the rows and columns of the matrix and displays the resulting transposed matrix. Understanding how to transpose matrices is important for various linear algebra operations and computational tasks in Python.

Leave a Comment

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

Scroll to Top