简体   繁体   中英

numpy advanced indexing on multidimensional-array

Say x is a 3x3 numpy array that contains the following:

import numpy as np
x = np.array([[ 1.,  2.,  3.],
              [ 4.,  5.,  6.],
              [ 7.,  8.,  9.]])

is there some indexing that can give me the following subarray:

array([[ 1.,  2.],
       [ 5.,  6.]])

You can use integer array indexing with a tuple of arrays:

>>> rows = np.array([[0, 0],
...                  [1, 1]], dtype=np.intp)
>>> columns = np.array([[0, 1],
...                     [1, 2]], dtype=np.intp)
>>> x[rows, columns]
array([[ 1.,  2.],
       [ 5.,  6.]])

you can use

x[:2,:2]

to solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM