Python: Differentiating between row and column vectors

前端 未结 12 1540
不知归路
不知归路 2020-12-07 08:56

Is there a good way of differentiating between row and column vectors in python? So far I\'m using numpy and scipy and what I see so far is that If I was to give one a vecto

12条回答
  •  旧巷少年郎
    2020-12-07 09:43

    If you want a distiction for this case I would recommend to use a matrix instead, where:

    matrix([1,2,3]) == matrix([1,2,3]).transpose()
    

    gives:

    matrix([[ True, False, False],
            [False,  True, False],
            [False, False,  True]], dtype=bool)
    

    You can also use a ndarray explicitly adding a second dimension:

    array([1,2,3])[None,:]
    #array([[1, 2, 3]])
    

    and:

    array([1,2,3])[:,None]
    #array([[1],
    #       [2],
    #       [3]])
    

提交回复
热议问题