Python Forum

Full Version: display single elements in array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have a 9 by 9 numpy array how do I display the 4 corner cell values:
Top left, top right, bottom left, bottom right.
Thanks
Use -1 index to get the last element in row/column, e.g.

>>> import numpy as np
>>> x=np.random.rand(10, 10)
>>> [x[0, 0], x[-1, 0], x[0, -1], x[-1, -1]]
You can ask for the corner values one at a time just by indexing into the array. And just like regular python lists, you can use the -1 index for the last value. So the corner values are [0,0], [0,-1], [-1,0], and [-1,-1], for any 2D array.

>>> niner = np.array(range(81)).reshape(9,9)
>>> print(niner[-1,0])
72
Or to combine them into a new array with advanced indexing, we can give all the rows we want and all the columns we want:

>>> print(niner[[0,0,-1,-1],[0,-1,0,-1]])
[ 0  8 72 80]
Thanks for this,
much appreciated