0

How transpose my matrix in python

A = [1, 2, 3, 4]

into:

B = [[1],
     [2],
     [3],
     [4]]??

A is numpy.array. When I transpose it by using A = A.T i get:

B = [[1,
      2,
      3,
      4]]

Thanks for help!

It must be exactly like:

B = [[1],
     [2],
     [3],
     [4]]

Not:

B = [[[1],
     [2],
     [3],
     [4]]]

Not:

 B = [[1]\n\n,[2]\n\n,[3]\n\n,[4]\n\n]

Look into debugger, not what is printed. U know what I mean?

  • Your `A` as displayed is a list, not numpy array. But if made into a list, its shape will be (4,) (1d). What you want has a shape of (4,1). You can either reshape that directly, or you can make a (1,4) shape array, and transpose that. This is not MATLAB where everything is 2d to start with. – hpaulj Dec 07 '18 at 19:53

1 Answers1

2

You could add a new axis:

import numpy as np

A = np.array([1, 2, 3, 4])
A = A[:, np.newaxis]
print(A)

Output

[[1]
 [2]
 [3]
 [4]]
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76