Skip to content Skip to sidebar Skip to footer

Numpy How Can I Get The Value Of An Element At Certain Coordinates?

I have found ways of getting the maximum value of a numpy array, and then getting the coordinates, but is there any way of doing the opposite? I mean, getting the value of an array

Solution 1:

The syntax is A[row_number,column_number]

The first row or column is numbered 0.

Here is an example:

In [1]: import numpy as np

In [2]: A = np.array([[1,2,3],[4,5,6],[7,8,9]])

In [3]: A
Out[3]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

In [4]: A[1,1]
Out[4]: 5

In [5]: A[2,1]
Out[5]: 8

In [6]: A[0,1]
Out[6]: 2

In [7]: A[2,2]
Out[7]: 9

And you can also use tuples for coordinates:

In [8]: coords = (0,0)

In [9]: A[coords]
Out[9]: 1

In [10]: newcoords = (2,2)

In [11]: A[newcoords]
Out[11]: 9

There are several other ways to read the elements of a numpy array. You may wish to read this Indexing documentation

Post a Comment for "Numpy How Can I Get The Value Of An Element At Certain Coordinates?"