Reputation: 11
Imagine, I have a 2D NumPy array X with 100 columns and 100 rows. Then, how can I extract the following rows and column using indexing?
rows= 1, 5, 15, 16 to 35, 45, 55 to 75
columns = 1, 2, 10 to 30, 42, 50
I know in MATLAB, we can do this using
X([1,5,15,16:35,45,55:75],[1,2,10:30,42,50]).
How can we do this in Python?
Upvotes: 1
Views: 30
Reputation: 150735
You can use np.r_
:
rows = np.r_[1, 5, 15, 16:35, 45, 55:75]
cols = np.r_[1, 2, 10:30, 42, 50]
X[rows,cols]
Note that, in Python, 16:35
usually does not include 35
. You may want to do 16:36
if you want row 35
as well.
Upvotes: 1