简体   繁体   中英

Java: bring value in [0,1] range

If I have a LinkedList of double with positive and negative value, for example:

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

How I can do to bring in [0,1] range?

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) .

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. Then scaling by (Xmax-Xmin) brings all values into the [0,1] range. 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.

/(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.

so (x-Xmin)/(Xmax-Xmin) has exactly the effect you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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