python - Conditional maths operation on 2D numpy array checking on one dimension and doing different operations on diff dimensions -


i have 2d numpy array column 0 pan rotation of device , column 1 tilt rotation. each row different fixture. want run following logic on each row:

if(pantilt[0] > 90):     pantilt[0] -=180     pantilt[1] *= -1 elif pantilt[0] < -90:     pantilt[0] += 180     pantilt[1] *= -1 

i understand basic conditional operations on 1d myarray[condition] = something. can't extrapolate more dimensions.

i calculate mask, or boolean index, , use if each column:

construct sample array:

pantilt=np.column_stack([np.linspace(-180,180,11),np.linspace(0,90,11)])  = pantilt[:,0]>90 # j = pantilt[:,0]<-90  pantilt[i,0] -= 180 pantilt[i,1] *= -1  = pantilt[:,0]<-90  # use j instead pantilt[i,0] += 180 pantilt[i,1] *= -1 

before:

array([[-180.,    0.],        [-144.,    9.],        [-108.,   18.],        [ -72.,   27.],        [ -36.,   36.],        [   0.,   45.],        [  36.,   54.],        [  72.,   63.],        [ 108.,   72.],        [ 144.,   81.],        [ 180.,   90.]]) 

after:

array([[  0.,  -0.],        [ 36.,  -9.],        [ 72., -18.],        [-72.,  27.],        [-36.,  36.],        [  0.,  45.],        [ 36.,  54.],        [ 72.,  63.],        [-72., -72.],        [-36., -81.],        [  0., -90.]]) 

this work if columns separate 1d arrays.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -