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?

1
  • 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. Commented Dec 7, 2018 at 19:53

1 Answer 1

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]]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.