numpy convert number to byte

后端 未结 2 1591
半阙折子戏
半阙折子戏 2021-01-22 19:35

I have a numpy matrix with float numbers mostly in range 0-255. However, there are some numbers that are a bit out of range (like -5.36, 270).

I want to convert the matr

2条回答
  •  耶瑟儿~
    2021-01-22 20:08

    You can use numpy.clip for that:

    a = np.arange(10)
    np.clip(a, 1, 8)
    > array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
    

提交回复
热议问题