The provided Python program is designed to transpose a given matrix. Transposing a matrix involves swapping its rows and columns. The program defines a function called transpose_matrix that takes a matrix as input and returns its transpose. The function creates a new matrix with dimensions swapped (number of rows becomes the number of columns and vice versa) and then iterates through the elements of the original matrix, assigning them to the corresponding positions in the transposed matrix. The example usage section demonstrates how to use this function with a sample matrix, where the original and transposed matrices are printed to the console. Users can replace the example_matrix with their own matrices to obtain the transpose of different matrices using this program.
Source code
def transpose_matrix(matrix):
# Get the number of rows and columns in the original matrix
rows, cols = len(matrix), len(matrix[0])
# Create a new matrix with swapped rows and columns
transposed_matrix = [[0 for _ in range(rows)] for _ in range(cols)]
# Populate the transposed matrix
for i in range(rows):
for j in range(cols):
transposed_matrix[j][i] = matrix[i][j]
return transposed_matrix
# Example usage:
# Replace this example matrix with your own matrix
example_matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
transposed_result = transpose_matrix(example_matrix)
# Print the original and transposed matrices
print("Original Matrix:")
for row in example_matrix:
print(row)
print("\nTransposed Matrix:")
for row in transposed_result:
print(row)
Description
Certainly! Here’s a step-by-step description of the provided Python program:
- Function Definition (
transpose_matrix):
- The program starts by defining a function called
transpose_matrixthat takes a matrix as an argument.
- Determine Matrix Dimensions:
- Inside the function, the number of rows and columns of the original matrix are obtained using
len(matrix)andlen(matrix[0]).
- Create Transposed Matrix:
- A new matrix called
transposed_matrixis created using a list comprehension with dimensions swapped.
- Populate Transposed Matrix:
- The program uses nested loops to iterate through the elements of the original matrix.
- For each element at position
(i, j)in the original matrix, the corresponding position in the transposed matrix is(j, i). - The value at position
(i, j)in the original matrix is assigned to position(j, i)in the transposed matrix.
- Return Transposed Matrix:
- The transposed matrix is returned from the function.
- Example Usage:
- An example matrix (
example_matrix) is provided for demonstration purposes. Users can replace this matrix with their own. - The
transpose_matrixfunction is called with the example matrix, and the result is stored intransposed_result.
- Print Original and Transposed Matrices:
- The program prints the original matrix and the transposed matrix separately.
- The original matrix is printed row by row using a loop.
- The transposed matrix is also printed row by row.
- Run the Program:
- When the program is executed, it will demonstrate the transposition of the example matrix and display both the original and transposed matrices.
Users can modify the example_matrix or use the transpose_matrix function with their own matrices to obtain the transposed result.
Screenshot

Output:

