How to normalize an assembly with a floating point, so that the sum of all elements is equal to X

Please forgive me if this question is incorrectly formulated or the solution is trivial. I can't seem to find an existing answer with conditions that I am familiar with.

I have an array of non-negative floats and I want to normalize them, which I can do. My problem: I would like the sum of all the elements in the collection to be a certain amount.

I can imagine ugly ways to achieve this, but I just know that there is a “right” way to do this. The goal is to create a composite histogram whose total width should be fixed. Each data point in the collection is assigned a color, which should receive N% of the total width of the bargraph. I am limited to this approach using the graph mapping method.

C # examples are preferred if code is needed.

// normalize data        
var ratio = 100.0 / widths.Max();
var normalizedList = widths.Select(o => o * ratio).ToList();
// magic happens here such that the sum of all elements is N
// and the relative scale of each sibling element is preserved

My huge and sincere thanks for any help,


additional information: The graph is a composite (segmented) histogram. Each element of the float collection corresponds to a segment. http://oi62.tinypic.com/2vskt8m.jpg

The necessity of the sum-of-all-elements-is-N rule relates to a graph drawing method, over which I have limited authority.

( , ), , , . , , , . , , .

+4
3

, . , .

int desiredTotal = 300; 

float[] widths = new float[] { 35f, 63f, 12f };

float ratio = desiredTotal / widths.Sum();
var normalizedList = widths.Select(o => o * ratio).ToList();

foreach (var item in normalizedList)
{
    Console.WriteLine(item);
}

Console.WriteLine(normalizedList.Sum());

/* Which prints:
95.45454
171.8182
32.72727
300
*/
+3

,

s = s0 + s1 + s2 +...

a = /

a * s = a * (s0 + s1 + s2 +...)

a * s = a * s0 + a * s1 + a * s2 +...

        var total = widths.Sum();
        var size=100;
        var ratio = size/total; //a in the equation
        var normalizedList = widths.Select(o => o * ratio).ToList();

        var normalSum = normalizedList.Sum(); //should be equal to size
+3

There may be more refined ways to approach this, but:

  • Normalize the entire set based on 1
  • Calculate the total amount of the standardized set as the Sum.
  • Multiply each item by (DesiredSum / Sum)

I have not tried this, just ran a couple of basic examples in my head, but it should cover it.

0
source

All Articles