Reputation: 287
Lets say I have a binary matrix, how do I find the (row,col)
location of the furthest north, south, east, and west with a value of 1
(or 0
).
Can this be easily transferred to finding the same furthest regions with a specific value in non binary matrices?
Upvotes: 0
Views: 96
Reputation: 14399
You can just look up the indices of any column or row that has a True
in it.
NS = np.where(np.any(M, axis=0))[0]
WE = np.where(np.any(M, axis=1))[0]
Take the first and last to get the extents:
N = NS[0]
S = NS[-1]
W = WE[0]
E = WE[-1]
For a non-boolean matrix M
you'd need to do some comparison that will output a boolean matrix, like:
NS = np.where(np.any(M > 0, axis=0))[0]
WE = np.where(np.any(M > 0, axis=1))[0]
Upvotes: 1