0

I need to make a function but do not know how. It is a part of a class matrix if that is important. I have a part code and what it needs to print out but I do not know how to make the function, can anyone help me.

This is the current code

m1 = Matrix()
m1.set_matrix( [[1,2,3,4],[5,6,7,8],[9,8,7,6]] )
print(m1)

This is the current output

[ 1 2 3 4 ][ 5 6 7 8 ][ 9 8 7 6 ]

but it needs to be in rows and columns, the matrix is supposed to be 3x4.

1 Answers1

0

This can be done in following ways:

  1. Using pandas: Convert matrix to pandas data-frame.
m1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,8,7,6]])
print(m1.T.to_numpy()) # Transpose the rows and columns

Result:

[[1 5 9]
 [2 6 8]
 [3 7 7]
 [4 8 6]]
  1. Using Numpy: Assuming it's a numpy matrix.
m = m1.to_numpy()
print(m)

[[1 2 3 4]
 [5 6 7 8]
 [9 8 7 6]]
print(m.transpose())

[[1 5 9]
 [2 6 8]
 [3 7 7]
 [4 8 6]]

Prashant Dey
  • 23
  • 2
  • 8