How do you preserve direction while fixing correction magnitude?
copysign combines the magnitude of its first operand with the sign of its second, element by element.
numpy.copysign in practice
Direction signals minus three and four give steps of minus and plus 0.5 at fixed magnitude. Zero also carries a sign, so explicitly handle zero if it should mean no motion.
import numpy as np
np.copysign(0.5,[-3,4])Result [-0.5, 0.5]
中文
怎样保留方向同时统一校正量的大小?
copysign 取第一个参数的绝对大小和第二个参数的符号,逐项组合结果。
numpy.copysign 算例
左右方向信号为 -3 和 4,固定一步为 0.5 得到 -0.5 和 0.5。零也带符号,需要零输入静止时应额外处理。
import numpy as np
np.copysign(0.5,[-3,4])结果 [-0.5, 0.5]