Range indexing in Numpy Array

Spaceratops

I’m having difficulty with finding a nice way of accessing the last elements with range-indexing of a numpy array.

In MATLAB, I would do this:

array(:,4:end) to grab all rows and columns 4 and up.

array[-1] in python will grab the last element, but array[:,3:-1] will from my testing not grab the last column (it would find all rows and columns 4 and up, except for the very last column, which is skipped).

Does anyone know of a shorthand way of indexing the last element when range-indexing so that I don’t have to figure it out myself every time I make an array? Thanks in advance.

I tried verifying if array[0:-1] carries the whole array, but it does not.

jared

When indexing an array in python, the slice is inclusive of the start and exclusive of the end. So, using arr[:, 3:-1] will return every row and columns 4 up to but not including the last column.

If you want to slice all the way to the end, leave the stop argument blank, i.e. write arr[:, 3:].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related