Best way to divide elements in a dictionary by the sum of columns C# -


i have dictionary of ints:

        dictionary<string, int[]> ret = new dictionary<string, int[]>();         int[] = {1,2,3,4,5};         int[] b = { 3, 6, 9, 10, 12 };         int[] c = {2,3,3,5,6};         ret.add("abc", a);         ret.add("def", b);         ret.add("ghi", c); 

what best way sum values in each of columns , divide total? example first value 1/{1+3+2}

here tried think sums rows instead , not work because type conversion error

public static dictionary<string, double[]> freq(dictionary<string, int[]> rawct)     {          dictionary<string, double[]> scores = new dictionary<string, double[]>();           foreach (keyvaluepair<string, int[]> kvp in rawct)         {             double[] percent = array.convertall(kvp.value, x => (double)x);             var total = (double)kvp.value.sum();              var result = percent.select(d => d / total);               scores.add(kvp.key, kvp.value);         }          return scores;     } 

if want divide columns, think can (assumming arrays of same size, in example, 5 itens):

dictionary<string, double[]> scores = ret.todictionary(r=> r.key,                          r => r.value.select((v, index)=>                           (double)v / ret.values.sum(values=> values[index]) // sum columns                          ).toarray()); 

output:

abc     0,166666666666667     0,181818181818182     0,2     0,210526315789474     0,217391304347826 def     0,5     0,545454545454545     0,6     0,526315789473684     0,521739130434783 ghi     0,333333333333333     0,272727272727273     0,2     0,263157894736842     0,260869565217391 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -