refle
refle

Reputation: 593

Crop part of np.array

Ihave a numpy array A like

A.shape
(512,270,1,20)

I dont want to use all the 20 layers in dimension 4. The new array should be like

Anew.shape
(512,270,1,2)

So I want to crop out 2 "slices" of the array A

Upvotes: 7

Views: 24795

Answers (2)

Chiel
Chiel

Reputation: 6194

From the python documentation, the answer is:

start = 4 # Index where you want to start.
Anew = A[:,:,:,start:start+2]

Upvotes: 10

ali_m
ali_m

Reputation: 74172

You can use a list or array of indices rather than slice notation in order to select an arbitrary sequence of indices in the final dimension:

x = np.zeros((512, 270, 1, 20))
y = x[..., [4, 10]] # the 5th and 11th indices in the final dimension
print(y.shape)
# (512,270,1,2)

Upvotes: 4

Related Questions