Home > Blockchain >  Numpy multi-dimensional array slicing
Numpy multi-dimensional array slicing

Time:10-03

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

CodePudding user response:

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])
  • Related