Python Forum
display single elements in array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
display single elements in array
#1
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
Reply
#2
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]]
Reply
#3
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]
Reply
#4
Thanks for this,
much appreciated
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to remove some elements from an array in python? gohanhango 9 1,139 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  How to display <IPython.core.display.HTML object>? pythopen 3 45,910 May-06-2023, 08:14 AM
Last Post: pramod08728
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,112 May-17-2022, 11:38 AM
Last Post: Larz60+
  Replace elements of array with elements from another array based on a third array Cola_Reb 6 1,831 May-13-2022, 06:06 PM
Last Post: deanhystad
Question Change elements of array based on position of input data Cola_Reb 6 2,111 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,584 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  4D array with only elements on one side of the diagonal schniefen 0 1,683 Dec-24-2020, 11:32 AM
Last Post: schniefen
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,223 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Removing some elements from array based on a condition claw91 0 1,506 Oct-27-2020, 03:42 PM
Last Post: claw91
  change array elements dependent on index SchroedingersLion 1 2,202 Nov-22-2019, 06:25 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020