简体   繁体   English

Java:将值带入[0,1]范围

[英]Java: bring value in [0,1] range

If I have a LinkedList of double with positive and negative value, for example: 如果我有一个具有正负两个值的double的LinkedList,例如:

[-1000, 34, 0, -700, 12]

How I can do to bring in [0,1] range? 我怎样才能带来[0,1]范围?

If I have only positive number I can do: 如果我只有正数,我可以做:

(x - Xmin) / (Xmax-Xmin)

But with both positive and negative value? 但是同时具有正值和负值吗?

The transformation remains the same: (x - Xmin) / (Xmax-Xmin) . 变换保持不变: (x - Xmin) / (Xmax-Xmin)

If you have negative numbers, subtracting the smallest of these is adding its magnitude to all values. 如果您有负数,则减去其中的最小数就是将其大小加到所有值上。 Xmin will end up at zero, anything larger will end up positive. Xmin最终将为零,更大的最终将为正。 Then scaling by (Xmax-Xmin) brings all values into the [0,1] range. 然后按(Xmax-Xmin)缩放将所有值置于[0,1]范围内。 Try a few cases by hand to see that it's so. 手动尝试几个案例,看看情况确实如此。

Your code should still work. 您的代码应该仍然有效。

x-Xmin moves all of your numbers so that they are all positive, with the smallest one equal to zero. x-Xmin移动您的所有数字,使它们均为正数,最小的一个等于零。

/(Xmax-Xmin) Scales the numbers so that the largest one is equal to 1, the smallest one stays at zero, and the ones in the middle are scaled proportionately. /(Xmax-Xmin)缩放数字,使最大的数字等于1,最小的数字为零,中间的数字按比例缩放。

so (x-Xmin)/(Xmax-Xmin) has exactly the effect you want. 因此(x-Xmin)/(Xmax-Xmin)具有您想要的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM