Open In App

Matrix Transpose Without Numpy In Python

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

[Tex]A^T = \begin{bmatrix} a_{11} & a_{21} \\ a_{12} & a_{22} \\ \end{bmatrix} [/Tex]

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)


Output

Original 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)


Output

Original 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)


Output

Original 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads