'I have a Matrix class were I need to implement a function, but I don't know how
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.
Solution 1:[1]
This can be done in following ways:
- 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]]
- 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]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Prashant Dey |
