Python - Calculate transposed matrix

Asked

Viewed 1,234 times

1

I need to calculate the transposed matrix using the python map function. But, I’m not able to solve.

def transp(a):
    for i in range(2):
        for j in a:
            return [a[i][j]]
        
list (map(transp,matrix))

The following error message appears - Typeerror: 'int' Object is not subscriptable

what I do to fix this bug?

  • Puts to us the declaration and settlement of matrix for us to test.

  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

4 answers

4

A solution

a = [[1, 2, 3, 4], [4, 6, 7, 9], [4, 3, 2, 1]]
*b, = zip(*a)
print(b)

Exit

[(1, 4, 4), (2, 6, 3), (3, 7, 2), (4, 9, 1)]

4

A solution using list comprehension:

A=[[1,2],[4,6]]

#printa as linhas da matriz A
for i in A:
    print(i)

A_t=[[k[0] for k in A],[k[1] for k in A]]

#printa as linhas da transposta de A
for i in A_t:
    print(i)

This solution uses the fact that an array can be seen as a list of lists and that transposition simply consists of transforming rows into columns.

Operations with arrays can become very complex depending on the application. For this reason I recommend studying implementations of this and other matrix operations in numpy and/or sympy modules.

4

Source matrix

M =[[1,2],[3,4],[5,6]]

Printing

for j in M:
    print(j)

Exit

[1, 2]
[3, 4]
[5, 6]

Transposed

Creating the matrix

M_t = list(map(list, zip(*M))) 

Printing

for j in M_t:
    print(j)

Exit

[1, 3, 5]
[2, 4, 6]

Using numpy

import numpy as np

M =[[1,2],[3,4],[5,6]]
M_t = np.array(M).T
  • 1

    Beautiful solution to see! What clean code!

  • Thanks @Matthew, big hug!

4

First, for you to calculate the transposed of an "A" matrix, you need the "A" matrix. Another thing, for you to calculate the transpose of a matrix with the help of the function map(), you will need to implement an anonymous function (lambda). In this case, the code can be implemented as follows:

a = [[1, 2], [4, 6]]
transposta = list(map(lambda *i: [j for j in i], *a))

print(transposta)

Note that for this code I used the matrix:

a = [[1, 2], [4, 6]]

When executing this code, we receive as a result:

[[1, 4], [2, 6]]

Just to reinforce what has already been said in the other answer, matrices is a very long subject and we can have better results if we study libraries better numpy and scipy.


If you want to implement a def function to calculate the transpose of the matrix you can implement the following code:

def transposta(m):
    return list(map(lambda *i: [j for j in i], *m))


matriz = [[1, 2], [4, 6]]

print(transposta(matriz))
  • The matrix was in the code, I just didn’t put this bit here, with respect to lambda function, I can only solve this problem with it? There’s no way to use def?

  • @Wesley Barros, at the level of Python code, there are several ways to calculate the transpose of a matrix. You can implement a function def to calculate the transpose of a matrix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.