Python中的转换矩阵
我在3D空间中有一个从-10到10的比例,步长为2.5(Python中的转换矩阵,python,python-3.x,math,matrix,interpolation,Python,Python 3.x,Math,Matrix,Interpolation,我在3D空间中有一个从-10到10的比例,步长为2.5([-10,-7.5,-5,-2.5,0,2.5,5,7.5,10])。 我在这个比例中有一个3D点,我想在0到8的另一个3D空间中映射它,步长为1([0,1,2,3,4,5,6,7,8]) 我怎么做 感谢您的帮助帮助您在一定范围内找到标准化的数学公式。python实现可以如下所示: def normalize(values, curr_bounds, new_bounds): return [(x - curr_bounds[0])
[-10,-7.5,-5,-2.5,0,2.5,5,7.5,10]
)。
我在这个比例中有一个3D点,我想在0到8的另一个3D空间中映射它,步长为1([0,1,2,3,4,5,6,7,8]
)
我怎么做
感谢您的帮助帮助您在一定范围内找到标准化的数学公式。python实现可以如下所示:
def normalize(values, curr_bounds, new_bounds):
return [(x - curr_bounds[0]) * (new_bounds[1] - new_bounds[0]) / (curr_bounds[1] - curr_bounds[0] + new_bounds[0]) for x in values]
您的请求不是很清楚,如果您的数据需要在离散范围内映射(逐步舍入),则您可以执行以下操作:
def normalize_step(values, curr_bounds, new_bounds, new_step):
return [round_step(new_step, (x - curr_bounds[0]) * (new_bounds[1] - new_bounds[0]) / (curr_bounds[1] - curr_bounds[0] + new_bounds[0])) for x in values]
def round_step(step, n):
return (n//step + 1) * step if n%step >= step/2 else n//step * step
例如,给定以下数据:
current_bounds = (-10, 10)
new_bounds = (0, 8)
step = 1
values = [-10, -2.5, 0, 7.5, 2.5]
normalize(values, current_bounds, new_bounds)
# [0.0, 3.0, 4.0, 7.0, 5.0]
normalize_step(values, current_bounds, new_bounds, step)
# [0.0, 3.0, 4.0, 7.0, 5.0]
注意:在这种情况下,结果是相同的,因为您的步骤=1
,如果我们将步骤=1.5
,结果会发生变化:
normalize_step(values, current_bounds, new_bounds, 1.5)
# [0.0, 3.0, 4.5, 7.5, 4.5]
您想要什么样的映射?它是线性映射吗?我不太清楚你在问什么。你能给出一个预期结果的例子吗?所以你想要类似于
value=value/2.5+4
?这取决于你如何表示你的3D矩阵。在某些情况下,矩阵和缩放矩阵相乘会更容易,例如使用numpy。你能举例说明矩阵变换前后的一些数据吗?