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.
>Solution :
In the example below, I have created a normal 2D python array and a copy numpy array and indexed all elements having index>=3 in numpy array. I hope it solves your issue.
import numpy as np
arr = [[1,2],[2,3],[4,5],[6,7],[8,9]]
np_arr = np.array(arr)
print(np_arr[3:, ])
[[6 7]
[8 9]]