1

An example is shown as follows:

>>> import numpy as np
>>> a=np.zeros((288,512))
>>> x1,x2,y1,y2=0,16,0,16
>>> p=a[x1:x2][y1:y2]
>>> p.shape
(16, 512)
>>> p=a[x1:x2,y1:y2]
>>> p.shape

I try to query a patch from an array, ranging from columns 0 to 16, and rows 0 to 16. I index the array in two ways and get very different result. a[x1:x2][y1:y2] gives me the wrong result.

Why?

Thx for helping me!!!

1
  • Those are different methods. With scalar indices they produce the same, but not with slices. Look at a[x1:x2] by itself. Then apply the y slice to that. Commented Oct 19, 2020 at 2:55

2 Answers 2

3

When you do a[x1:x2][y1:y2], you are slicing by rows twice. That is, a[x1:x2] will give you a shape (16,512). The second slice operation in a[x1:x2][y1:y2] is slicing the result of the first operation and will give you the same result.

In the second case, when you do a[x1:x2,y1:y2], you are slicing by the two dimensions of your 2-dimensional array.

Important note: If you have a 2-dimensional array and you slice like this:

a = np.zeros((10,15))
a[1:3].shape

Output:
(2, 15)

you will slice only by rows. Your resulting array will have 2 rows and the total number of columns (15 columns). If you want to slice by rows and columns, you will have to use a[1:3, 1:3].

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

Comments

1

The two methods of indexing you tried are not equivalent. In the first one (a[x1:x2][y1:y2]), you are essentially indexing the first axis twice. In the second, you are indexing the first and second axes.

a[x1:x2][y1:y2] can be rewritten as

p = a[x1:x2] # result still has two dimensions
p = p[y1:y2]

You are first indexing 0:16 in the first dimension. Then you index 0:16 in the first dimension of the result of the previous operation (which will simply return the same as a[x1:x2] because x1==y1 and x2==y2).

In the second method, you index the first and second dimensions directly. I would not write it this way, but one could write it like this to contrast it with the first method:

a[x1:x2][:, y1:y2]

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.