J.Doe
J.Doe

Reputation: 13

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.]])

Upvotes: 1

Views: 258

Answers (2)

MSeifert
MSeifert

Reputation: 152870

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.]])

Upvotes: 4

Po Stevanus Andrianta
Po Stevanus Andrianta

Reputation: 712

you can use

x[:2,:2]

to solve your problem

Upvotes: -3

Related Questions