Accord.Statistics.Measures.HistogramMedian C# (CSharp) Method

HistogramMedian() public static method

Calculate median value of an histogram.

The input array is treated as histogram, i.e. its indexes are treated as values of stochastic function, but array values are treated as "probabilities" (total amount of hits).

The median value is calculated accumulating histogram's values starting from the left point until the sum reaches 50% of histogram's sum.

public static HistogramMedian ( this values ) : int
values this Histogram array.
return int
        public static int HistogramMedian(this int[] values)
        {
            int total = 0;

            // for all values
            for (int i = 0; i < values.Length; i++)
            {
                // accumulate total
                total += values[i];
            }

            int halfTotal = total / 2;
            int median = 0;
            int v = 0;

            // find median value
            for (; median < values.Length; median++)
            {
                v += values[median];
                if (v >= halfTotal)
                    break;
            }

            return median;
        }