How to select elements in numpy array with given starting point indices

前端 未结 2 723
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 05:45

For example, I have a matrix like this:

In [2]: a = np.arange(12).reshape(3, 4)

In [3]: a
Out[3]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,         


        
2条回答
  •  醉话见心
    2021-01-28 06:18

    An expected, exact, output is not provided. So, I think the followings may help you in general.

    >>> a = np.arange(12).reshape(3, 4)
    >>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    >>> row = np.array([0, 1, 2])
    >>> col = np.array([1, 2, 0])
    >>> a[row, col]
    array([1, 6, 8])
    

    You can set the row and cols of a to an value:

    >>> a[row, col] = 0
    >>> a
    array([[ 0,  0,  2,  3],
           [ 4,  5,  0,  7],
           [ 0,  9, 10, 11]])
    

提交回复
热议问题