4

Given this array:

>>> a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

How can I select [[4,5], [7,8]]? a[0::2, 1:;2] doesn't work

4
  • print a[1:3, 1:3] would work fine Commented May 27, 2016 at 4:49
  • what about select N elements from a given index? Commented May 27, 2016 at 4:52
  • It depends on the context of index, it a 1D array , 2D array or an ND array. Commented May 27, 2016 at 4:53
  • Same as the example above. Say select 2 elements from each dimension of an ND array given a N-tuple Commented May 27, 2016 at 4:54

1 Answer 1

6
>>> a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> a[1:3,1:3]
array([[4, 5],
       [7, 8]])

The first 1:3 is to select row 1 & 2. The second 1:3 is to select column 1 & 2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.