我有一个2D numpy数组,其中列0是设备的平移旋转,列1是倾斜旋转。每行是不同的灯具。我想在每一行上运行以下逻辑:
if(pantilt[0] > 90):
pantilt[0] -=180
pantilt[1] *= -1
elif pantilt[0] < -90:
pantilt[0] += 180
pantilt[1] *= -1
我了解一维的基本条件运算,例如myarray [condition] =某物。但是我无法将其推断出更多的维度。
python大神给出的解决方案
我将计算一个掩码或布尔值索引,并对每个列使用if:
构造一个样本数组:
pantilt=np.column_stack([np.linspace(-180,180,11),np.linspace(0,90,11)])
I = pantilt[:,0]>90
# J = pantilt[:,0]<-90
pantilt[I,0] -= 180
pantilt[I,1] *= -1
I = pantilt[:,0]<-90 # could use J instead
pantilt[I,0] += 180
pantilt[I,1] *= -1
之前:
array([[-180., 0.],
[-144., 9.],
[-108., 18.],
[ -72., 27.],
[ -36., 36.],
[ 0., 45.],
[ 36., 54.],
[ 72., 63.],
[ 108., 72.],
[ 144., 81.],
[ 180., 90.]])
后:
array([[ 0., -0.],
[ 36., -9.],
[ 72., -18.],
[-72., 27.],
[-36., 36.],
[ 0., 45.],
[ 36., 54.],
[ 72., 63.],
[-72., -72.],
[-36., -81.],
[ 0., -90.]])
如果列是单独的1d数组,则效果也会很好。