Matrix Transpose in Python

前端 未结 18 1640
甜味超标
甜味超标 2020-11-22 00:21

I am trying to create a matrix transpose function for python but I can\'t seem to make it work. Say I have

theArray = [[\'a\',\'b\',\'c\'],[\'d\',\'e\',\'f\         


        
18条回答
  •  清歌不尽
    2020-11-22 01:12

    If you want to transpose a matrix like A = np.array([[1,2],[3,4]]), then you can simply use A.T, but for a vector like a = [1,2], a.T does not return a transpose! and you need to use a.reshape(-1, 1), as below

    import numpy as np
    a = np.array([1,2])
    print('a.T not transposing Python!\n','a = ',a,'\n','a.T = ', a.T)
    print('Transpose of vector a is: \n',a.reshape(-1, 1))
    
    A = np.array([[1,2],[3,4]])
    print('Transpose of matrix A is: \n',A.T)
    

提交回复
热议问题