0

I have a u0 list in which values are appended, and then I have to take the transpose of u0, but when I am doing this, I still get a row matrix, but I want to show it as a column matrix. From other answers, I learned that there would be no difference in calculations, but I want it to be shown as column matrix and this can be used further for calculations. x_mirror is also a list.

u0 = []
for p in range(0,tpts):                              #initial condition at t = 0
    ts =  ((np.exp(alpha*((x_mirror[p]-x_start)**2))*(np.cos(k0*(x_mirror[p]-x_start)))))
    u0.append(ts)

Any help is highly appreciated.

Avii
  • 164
  • 12

4 Answers4

3

Based on an already existing stackoverflow post (here) you can do the following to

import numpy as np
list1 = [2,4,6,8,10]

array1 = np.array(list1)[np.newaxis]
print(array1)
print(array1.transpose())

For the above code you can see the output here:

enter image description here

Shankar
  • 126
  • 7
1

If you just want it to display vertically, you can create your class as a subclass of list.

class ColumnList(list):
    def __repr__(self):
        parts = '\n '.join(map(str, self))
        r = f'[{parts}]'
        return r

u0 = ColumnList()
for p in range(0,tpts):                              #initial condition at t = 0
    ts =  ((np.exp(alpha*((x_mirror[p]-x_start)**2))*(np.cos(k0*(x_mirror[p]-x_start)))))
    u0.append(ts)

# displays vertically:
u0
James
  • 32,991
  • 4
  • 47
  • 70
  • Thanks for the help. Suppose if we want the transposed matrix to be used for further calculations especially matrix multiplication, how can we do that? will there be much difference in the answers? – Avii Dec 30 '21 at 13:55
1

You can use reshape -

u0 = [1, 2, 3]
np_u0_row = np.array(u0)
np_u0_col = np_u0_row.reshape((1, len(u0)))
print(np_u0_col.shape)
# (1, 3)
Mortz
  • 4,654
  • 1
  • 19
  • 35
-1

Not sure if I got the question right, but try to convert it into a 2-dimensional Array with the right number of rows and only one column.

rowMatrix = [1, 2, 3, 4, 5]
columnMatrix = [[1], [2], [3], [4], [5]]
fhirsch
  • 9
  • 1