我有一個1d數組,並且想要查找像這樣的最後一個值。查找numpy數組中的最後一個值
a = np.array([1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1])
# find the index that value(7) last appear.
np.argwhere(a >= 7).max()
# output 10
但它適合1d陣列和3D陣列。
b = np.tile(a.reshape(15,1,1), reps=(1,30,30))
# now b is 3d array and i want to use same way to the axis = 0 in 3d array.
np.argwhere(b >= 7)
# return a 2d array. It's not what i need.
雖然我可以用「for」循環的另一個軸,但我想通過numpy的有效解決。
@StephenRauch對不起,應該是適用的.max() –
既沒有張貼的解決方案,爲你工作? – Divakar
@Divakar謝謝,它的作品。但是有一個問題,如果數組中不存在數值(例如a == 7)。它返回最大數組索引,如何解決它? –