Home python Transpose a matrix (numpy array) in python

Transpose a matrix (numpy array) in python

Author

Date

Category

I have:

import numpy as np
a = np.array ([0, 1, 2], [4, 5, 6])

and now how can I transpose this matrix to get

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

?


Answer 1, authority 100%

Transpose an array in numpy like this:

import numpy as np
a = np.array ([[0, 1, 2], [4, 5, 6]])
a = a.transpose ()
print (a)

Result:

[[0 4]
 [15]
 [2 6]]

Answer 2, authority 75%

import numpy as np
a = np.array ([0, 1, 2], [4, 5, 6])
a = a.T

Answer 3

def transpouse (mat):
  matrix = []
  for i in range (len (mat [0])): # mat is the original matrix
    matrix.append (list ())
    for j in range (len (mat)):
      matrix [i] .append (mat [j] [i])
  return matrix

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions