How to normalize a list of int values

后端 未结 3 1203
粉色の甜心
粉色の甜心 2020-12-16 02:53

I have a list of int values:

List histogram;

How do I normalize all values so that the max value in the

3条回答
  •  借酒劲吻你
    2020-12-16 03:25

    If you have a list of strictly positive numbers, then Dav's answer will suit you fine.

    If the list can be any numbers at all, then you need to also normalise to a lowerbound.

    Assuming an upper bound of 100 and a lower bound of 0, you'll want something like this ...

    var max = list.Max();
    var min = list.Min();
    var range = (double)(max - min);
    var normalised 
        = list.Select( i => 100 * (i - min)/range)
            .ToList();
    

    Handling the case where min == max is left as an exercise for the reader ...

提交回复
热议问题