catrev
catrev

Reputation: 85

NUMPY: select even lines, last column

I am trying to learn numpy and I can't manage to complete this question: take the even lines, last column of the M matrix:

 [[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]
 [26 27 28 29 30]]

What I did : print(M[0:, -1, 2], '\n') error: IndexError: too many indices for array

Why isn't this working ? I select all the lines with 0:, the last column with -1, with step 2.

Upvotes: 0

Views: 82

Answers (1)

Djaouad
Djaouad

Reputation: 22776

Your array is 2-dimensional, but you're using three indices as if your array had 3 dimensions, you can use this index to get what you want:

print(M[::2, -1])

Output:

[ 5 15 25]

Upvotes: 2

Related Questions