Kenny Barker Kenny Barker - 1 month ago 11
Vb.net Question

VB.NET Normalizing Data [1, -1]

I am currently using the following code to normalize data:

Public Function NormalizeData(values As Double()) As Double()
Dim min = values.Min
Dim max = values.Max
Dim outList(values.Count - 1) As Double
For i = 0 To values.Count - 1
outList(i) = (values(i) - min) / (max - min)
Next
Return outList
End Function


This returns values from 0 to 1 accordingly. I am confused on how to make this normalize between [1, -1] instead of [1, 0].

Answer Source

You already know how to normalise to a range of 0 to 1. If you want the range to be -1 to 1, you just need to multiply the normalised data by 2 and subtract 1.

As I said in my comment, you could just modify the statement inside your For loop to adjust the data:

outList(i) = 2 * (values(i) - min) / (max - min) - 1

However, rather than use a loop to build the second array, I would make the code more compact by using the Select method to return a new array with modified values. The whole function would then be:

Public Function NormalizeData(values As Double()) As Double()
    Dim min = values.Min
    Dim max = values.Max
    Return values.Select(Function(val) 2 * (val - min) / (max - min) - 1).ToArray
End Function