How to normalize a list of int values

后端 未结 3 1210
粉色の甜心
粉色の甜心 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:34

    To normalize a set of numbers that may contain negative values,
    and to define the normalized scale's range:

    List list = new List{-5,-4,-3,-2,-1,0,1,2,3,4,5};
    double scaleMin = -1; //the normalized minimum desired
    double scaleMax = 1; //the normalized maximum desired
    
    double valueMax = list.Max();
    double valueMin = list.Min();
    double valueRange = valueMax - valueMin;
    double scaleRange = scaleMax - scaleMin;
    
    IEnumerable normalized = 
        list.Select (i =>
            ((scaleRange * (i - valueMin))
                / valueRange)
            + scaleMin);
    

提交回复
热议问题