Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Range indexing in Numpy Array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading