numpy how can i get the value of an element at certain coordinates?

前端 未结 1 1028
甜味超标
甜味超标 2021-01-29 12:30

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

相关标签:
1条回答
  • 2021-01-29 13:04

    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

    0 讨论(0)
提交回复
热议问题