我有一个3D的numpy数组,并希望根据另一个元素的条件测试来更改特定元素。(这个应用是为了更改RGBA图像数组中的“alpha”,以调整3D pyqtgraph 图像中的透明度,最好能够快速实现)。
现在我想有条件地测试最低维度的第一个元素,然后根据结果更改最后一个元素。
a= np.ones((2,4,5),dtype=np.int) #create a ones array
a[0,0,0] = 3 #change a few values
a[0,2,0] = 3
a[1,2,0] = 3
print(a)
>>>[[[3 1 1 1 1]
[1 1 1 1 1]
[3 1 1 1 1]]
[[1 1 1 1 1]
[1 1 1 1 1]
[3 1 1 1 1]]]
现在我想有条件地测试最低维度的第一个元素,然后根据结果更改最后一个元素。
if a[0,:,:] > 1: #this does not work - for example only - if first element > 1
a[3,:,:]=255 #then change the last element in the same dimension to 255
print(a) #this is my idealized result
>>>[[[3 1 1 1 255]
[1 1 1 1 1]
[3 1 1 1 255]]
[[1 1 1 1 1]
[1 1 1 1 1]
[3 1 1 1 255]]]
a[mask,:,4] = 255
或a[mask,:,-1] = 255
。 - Daniel