Kun
Kun

Reputation: 591

Numpy multi-dimensional array slicing

I have a 3-D NumPy array with shape (100, 50, 20). I was trying to slice the third dimension of the array by using the index, e.g., from 1 to 6 and from 8 to 10.

I tried the following code, but it kept reporting a syntax error.

newarr [:,:,1:10] = oldarr[:,:,[1:7,8:11]]

Upvotes: 0

Views: 261

Answers (1)

akuiper
akuiper

Reputation: 214927

You can use np.r_ to concatenate slice objects:

newarr [:,:,1:10] = oldarr[:,:,np.r_[1:7,8:11]] 

Example:

np.r_[1:4,6:8]
array([1, 2, 3, 6, 7])

Upvotes: 1

Related Questions