Home > Software engineering >  Unable to transpose an array using .T
Unable to transpose an array using .T

Time:01-24

This should work but the dimension of c and new_c is always the same. c is an array of dim (13,1) and c_new is also an array of (13,1)

import numpy as np
import matplotlib.pylab as plt
n = np.arange(1, 26, 2, dtype=float)
c = 4 / (np.pi * n)
new_c = c.T

CodePudding user response:

n is of shape (13,) which is different from (13,1). (n,) means that it only has a single dimension, which means that there are no dimensions that transpose can swap. If you do

n = np.arange(1, 26, 2, dtype=float).reshape(-1,1)

you get what you wanted to have. There are also other ways of achieving this like

n = np.arange(1, 26, 2, dtype=float)[:,None]

or

n = np.expand_dims(np.arange(1, 26, 2, dtype=float), axis=1)

CodePudding user response:

Or np.newaxis:

n = np.arange(1, 26, 2, dtype=float)[:, np.newaxis]
  •  Tags:  
  • Related