Issue
I have an array like:
val=[[1,1,1,0,0,1,1,0,0,0], [0,1,1,1,0,1,1,0,0,0]]
I would like to access the indices where the value changes from 1 to 0.
Expected output is like:
[[(2,3),(4,5),(6,7)],[(0,1),(3,4),(4,5),(6,7)]]
I tried with np.gradient function, and able to find the gradient value. Instead of this is any better method:
X=np.gradient(val,axis=1)
Y=np.gradient(val,axis=0)
trans_YX = np.array(list(zip(Y.ravel(),X.ravel())),dtype('f4,f4')).reshape(Y.shape)
Current output:
[[(-1., 0. ) ( 0., 0. ) ( 0., -0.5) ( 1., -0.5) ( 0., 0.5) ( 0., 0.5)
( 0., -0.5) ( 0., -0.5) ( 0., 0. ) ( 0., 0. )]
[(-1., 1. ) ( 0., 0.5) ( 0., 0. ) ( 1., -0.5) ( 0., 0. ) ( 0., 0.5)
( 0., -0.5) ( 0., -0.5) ( 0., 0. ) ( 0., 0. )]]
Solution
If pure python works for our solution. you can apply the methodology to each row:
[(i, i + 1) for i in range(len((val_row)) - 1) if val_row[i] != val_row[i+1]]
Answered By - Muhammad Ahsan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.