Matrix Transpose Without Numpy In Python
Last Updated :
26 Feb, 2024
Matrix transpose is a fundamental operation in linear algebra where the rows of a matrix are swapped with its columns. This operation is denoted by A^T, where A is the original matrix. The transposition of a matrix has various applications, such as solving systems of linear equations, computing the inner product of matrices, and more
What is Matrix Transpose?
A transpose of a matrix is an operation that switches the positions of its rows and columns. If you have a matrix A with dimensions m×n, the transpose A^T will have dimensions n×m, and the elements of A^T will be such that the i-th row of A^T is the i-th column of A. Here, I'll provide a general explanation using mathematical notation and Python syntax:
A^T =
\begin{bmatrix}
a_{11} & a_{21} \\
a_{12} & a_{22} \\
\end{bmatrix}
Matrix Transpose Without Numpy in Python
Below, are the methods of Matrix Transpose Without Numpy In Python.
Matrix Transpose Without Numpy Using Nested Loops
In this example, below Python code transposes a given matrix by initializing an empty matrix 'transposed' with dimensions swapped. It then iterates through the original matrix, assigning the transposed values to the new matrix. Finally, it prints both the original and transposed matrices for comparison.
Python3
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
rows = len(matrix)
cols = len(matrix[0])
transposed = [[0 for _ in range(rows)] for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transposed[j][i] = matrix[i][j]
print("Original Matrix:")
for row in matrix:
print(row)
print("\nTransposed Matrix:")
for row in transposed:
print(row)
OutputOriginal Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Matrix Transpose Without Numpy Using List Comprehension
In this example, below Python code efficiently transposes a given matrix using list comprehension and zip. It creates the transposed matrix by extracting columns from the original matrix. The code then prints both the original and transposed matrices for comparison.
Python3
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
print("Original Matrix:")
for row in matrix:
print(row)
print("\nTransposed Matrix:")
for row in transposed:
print(row)
OutputOriginal Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Matrix Transpose Without Numpy Using zip() Function
In this example, below Python code efficiently transposes a given matrix using the zip function with the unpacking operator (*). It creates the transposed matrix by unpacking columns from the original matrix. The code then prints both the original and transposed matrices for comparison.
Python3
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
transposed = [list(row) for row in zip(*matrix)]
print("Original Matrix:")
for row in matrix:
print(row)
print("\nTransposed Matrix:")
for row in transposed:
print(row)
OutputOriginal Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transposed Matrix:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Conclusion
In Conclusion , above Python processes efficiently achieve matrix transposition without relying on NumPy. The first uses nested loops to swap rows and columns. The second employs nested list comprehension for a more compact solution. The third utilizes the zip function with unpacking, offering a concise and elegant approach. Despite their implementation differences, all approaches effectively manipulate matrices, catering to various preferences for readability and brevity.
Similar Reads
How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin
3 min read
How to create a constant matrix in Python with NumPy? A matrix represents a collection of numbers arranged in the order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses or brackets. A constant matrix is a type of matrix whose elements are the same i.e. the element does not change irrespective of any index value th
4 min read
numpy.zeros() in Python numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results. We can create 1D array using numpy.zeros().Let's understand with the help of an example:Pythonimport
2 min read
NumPy Tutorial - Python Library NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
NumPy Tutorial - Python Library NumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Raise a square matrix to the power n in Linear Algebra using NumPy in Python In this article, we will discuss how to raise a square matrix to the power n in the Linear Algebra in Python. The numpy.linalg.matrix_power() method is used to raise a square matrix to the power n. It will take two parameters, The 1st parameter is an input matrix that is created using a NumPy array
3 min read