SoFunction
Updated on 2024-10-30

Python implementation of several methods of matrix transposition details

Python implementation of matrix transpose

Transposing a matrix means that we change its columns into rows. Let's understand it through an example of what it looks like if transposed.

Assuming you have the original matrix, for example -x = [[1,2][3,4][5,6]]

In the matrix " x" above, we have two columns, 1, 3, 5 and 2, 4, 6.

Thus, when we transpose above the matrix "x", the columns become rows. Thus, the transposed version of the matrix above looks like -x1 = [[1, 3, 5][2, 4, 6]]

Thus, we have another matrix " x1 ", organized differently with different values at different locations.

Method 1, using numpy transpose

import numpy as np
A = ([[1,2,3],[4,5,6],[7,8,9]])
print()
print((0, 1))

# Both outputs
# [[1 4 7]
#  [2 5 8]
#  [3 6 9]]

import numpy as np
A = [[1,2,3],[4,5,6],[7,8,9]]
print((A))

# Output
# [[1 4 7]
#  [2 5 8]
#  [3 6 9]]

Method two, using the zip() function

  • The zip() function is used to take an iterable object as an argument, pack the corresponding elements of the object into a tuple, and then return the object composed of those tuples, which has the advantage of saving a lot of memory.
  • You can use the list() conversion to output a list. Differences between the zip method in Python 2 and Python 3: In Python, zip() returns an object to minimize memory. To display a list, you need to convert list() manually.]
  • If the number of elements is not the same across iterators, the list is returned with the same length as the shortest object, utilizing the* No. operator that decompresses a tuple into a list.

zip(A) Equivalent to packing, packing isList of tuples

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> A = zip(a,b)     # Packed as a list of tuples
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c)              # of elements to match the shortest list
[(1, 4), (2, 5), (3, 6)]
>>> zip(*A)          # In contrast to zip, *A can be interpreted as decompression and returns a two-dimensional matrix equation
[(1, 2, 3), (4, 5, 6)]
A = [[1,2,3],[4,5,6],[7,8,9]]
print(*A) #[1, 2, 3] [4, 5, 6] [7, 8, 9]
#zip() returns an object. To display a list, you need to convert list() manually.
#print(zip(*A)) #<zip object at 0x000001CD7733A2C8>
print(list(zip(*A)))

# Output
# [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Here the asterisk (*) in python serves to disassemble the elements of an iterable object in a variable.

Method 3: Using python list expressions

Doesn't take up extra space, "modifies in place"

A = [[1,2,3],[4,5,6],[7,8,9]]
#print(len(A)) #matrix rows
#print(len(A[0])) #matrix columns
B = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
print(B)

# Output
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

B = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]

A clearer way to write this sentence is:

A = [[1,2,3],[4,5,6],[7,8,9]]
#print(len(A)) #matrix rows
#print(len(A[0])) #matrix columns
for i in range(len(A[0])):#len(A[0]) number of matrix columns
    for j in range(i,len(A)):#len(A) number of matrix rows
        #Transpose is A[i][j] and A[j][i] interchanged
        A[j][i], A[i][j] = A[i][j], A[j][i]
print(A)

# Output
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Because of the symmetry of the transposed matrixfor j in range(i,len(A)) The restriction to traverse only the upper triangles of the matrix must be restricted, as failure to do so will result in repeated exchanges.

Method 4: Create a new list B and add elements using a double loop

A = [[1,2,3],[4,5,6],[7,8,9]]
B=[]
for i in range(len(A[0])):#len(A[0]) number of matrix columns
    temp = []
    for j in range(len(A)):#len(A) number of matrix rows
        (A[j][i])
    (temp)
print(B)

# Output
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

This article on the Python implementation of the matrix transpose several ways to explain the article is introduced to this, more related Python implementation of the matrix transpose content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!