🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Transposing a matrix involves swapping rows with columns. If you have a matrix that is m x n (m rows and n columns), its transpose will be n x m (n rows and m columns). Transposition is a fundamental operation in linear algebra and finds its applications in various domains, from computer graphics to machine learning. In this post, we will explore how to transpose a matrix in Python.
2. Program Overview
1. Define the matrix.
2. Use list comprehension to transpose the matrix.
3. Display the original and transposed matrix.
3. Code Program
# Python program to transpose a matrix
# Sample matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
def transpose_matrix(mat):
"""Function to transpose a given matrix."""
# Use list comprehension to get the transpose
transposed = [[mat[j][i] for j in range(len(mat))] for i in range(len(mat[0]))]
return transposed
# Get the transposed matrix
transposed_matrix = transpose_matrix(matrix)
# Display the matrices
print("Original Matrix:")
for row in matrix:
print(row)
print("\nTransposed Matrix:")
for row in transposed_matrix:
print(row)
Output:
Original Matrix: [1, 2, 3] [4, 5, 6] [7, 8, 9] Transposed Matrix: [1, 4, 7] [2, 5, 8] [3, 6, 9]
4. Step By Step Explanation
1. A sample matrix of size 3x3 is defined for demonstration. This matrix can be changed to any other size or value.
2. The transpose_matrix function takes a matrix as an argument and uses list comprehension to generate the transposed matrix. It loops through columns for the outer loop and rows for the inner loop, essentially flipping the positions of elements.
3. The original and transposed matrices are then displayed using a simple loop.
4. In the provided output, the original matrix has its first row as [1, 2, 3], while the transposed matrix has its first column as [1, 2, 3], indicating the successful transposition of the matrix.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment