Indexing numpy array with another numpy array

后端 未结 2 545
说谎
说谎 2020-12-15 04:16

Suppose I have

a = array([[1, 2],
           [3, 4]])

and

b = array([1,1])

I\'d like to use b in index a

相关标签:
2条回答
  • 2020-12-15 04:56

    Suppose you want to access a subvector of a with n index pairs stored in blike so:

    b = array([[0, 0],
           ...
           [1, 1]])
    

    This can be done as follows:

    a[b[:,0], b[:,1]]
    

    For a single pair index vector this changes to a[b[0],b[1]], but I guess the tuple approach is easier to read and hence preferable.

    0 讨论(0)
  • 2020-12-15 05:00

    According the numpy tutorial:

    a[tuple(b)] 
    

    is the correct way to do this:

    http://www.scipy.org/Tentative_NumPy_Tutorial#head-3f4d28139e045a442f78c5218c379af64c2c8c9e

    0 讨论(0)
提交回复
热议问题