41_ Andi Yuda
41_ Andi Yuda

Reputation: 21

how to extract (x, y) coordinate value from a grid python

I have a grid 7x3 how to extract (x, y) coordinate value that contain "1"?

grid = np.array([
[0,0,0,0,0,0,0],
[0,0,1,0,1,0,1],
[0,0,1,0,1,0,1]])

Upvotes: 0

Views: 357

Answers (1)

Mark
Mark

Reputation: 92440

You can pass your array to numpy.argwhere which will find non-zero values:

grid = np.array([
    [0,0,0,0,0,0,0],
    [0,0,1,0,1,0,1],
    [0,0,1,0,1,0,1]
])

np.argwhere(grid)

returning

array([[1, 2],
       [1, 4],
       [1, 6],
       [2, 2],
       [2, 4],
       [2, 6]])

If you need specifically where the array is 1 and not non-zero, you can pass a condition:

np.argwhere(grid == 1)

giving the same result.

Upvotes: 2

Related Questions