Normalizing an Array between 0 and 1

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godot_Starter

So I have an array like this for example:

var some_array = [1,2,-2,1.5]

And I want to normalize this array so that the highest value becomes 1 and the lowest 0 and the other values something in between like this:

var new_array = [0.75,1,0,0.875]

Are there any functions that can help me here or do i have to do it mathematicly?

:bust_in_silhouette: Reply From: sash-rc
func normalize_array(array : Array) -> void:
  var minVal  = array.min()
  var maxVal  = array.max()
  for i in range(0, array.size()):
    array[i] = range_lerp(array[i], minVal, maxVal, 0.0, 1.0)
1 Like