Median value from double array - CSharp System

CSharp examples for System:Array Calculation

Description

Median value from double array

Demo Code

/// Department of Proteomics and Signal Transduction. All rights reserved.
using System.Collections.Generic;
using System;/*from  w ww . ja v  a2s .c o  m*/

public class Main{
        private static double Median(double[] m, int min, int max)
        {
            int len = max - min + 1;
            if (len == 1)
            {
                return m[min];
            }
            if (len == 2)
            {
                return 0.5f * (m[min] + m[max]);
            }
            if (len == 3)
            {
                double m1 = m[min];
                double m2 = m[min + 1];
                double m3 = m[min + 2];
                if (m1 <= m2 && m2 <= m3)
                {
                    return m2;
                }
                if (m2 <= m3 && m3 <= m1)
                {
                    return m3;
                }
                if (m3 <= m1 && m1 <= m2)
                {
                    return m1;
                }
                if (m3 <= m2 && m2 <= m1)
                {
                    return m2;
                }
                if (m2 <= m1 && m1 <= m3)
                {
                    return m1;
                }
                if (m1 <= m3 && m3 <= m2)
                {
                    return m3;
                }
            }
            double[] x = new double[len];
            for (int i = 0; i < len; i++)
            {
                x[i] = m[min + i];
            }
            Array.Sort(x);
            if (len % 2 == 0)
            {
                int w = len / 2;
                return 0.5f * (x[w - 1] + x[w]);
            }
            else
            {
                int w = len / 2;
                return x[w];
            }
        }
        private static float Median(float[] m, int min, int max)
        {
            int len = max - min + 1;
            if (len == 1)
            {
                return m[min];
            }
            if (len == 2)
            {
                return 0.5f * (m[min] + m[max]);
            }
            if (len == 3)
            {
                float m1 = m[min];
                float m2 = m[min + 1];
                float m3 = m[min + 2];
                if (m1 <= m2 && m2 <= m3)
                {
                    return m2;
                }
                if (m2 <= m3 && m3 <= m1)
                {
                    return m3;
                }
                if (m3 <= m1 && m1 <= m2)
                {
                    return m1;
                }
                if (m3 <= m2 && m2 <= m1)
                {
                    return m2;
                }
                if (m2 <= m1 && m1 <= m3)
                {
                    return m1;
                }
                if (m1 <= m3 && m3 <= m2)
                {
                    return m3;
                }
            }
            float[] x = new float[len];
            for (int i = 0; i < len; i++)
            {
                x[i] = m[min + i];
            }
            Array.Sort(x);
            if (len % 2 == 0)
            {
                int w = len / 2;
                return 0.5f * (x[w - 1] + x[w]);
            }
            else
            {
                int w = len / 2;
                return x[w];
            }
        }
        public static double Median(double[] x)
        {
            int n = x.Length;
            if (n == 0)
            {
                return double.NaN;
            }
            int[] o = Order(x);
            if (n % 2 == 1)
            {
                return x[o[n / 2]];
            }
            return 0.5 * (x[o[n / 2 - 1]] + x[o[n / 2]]);
        }
}

Related Tutorials