Reputation: 21
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
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