Returns MAD - median of the absolute values - Java java.lang

Java examples for java.lang:Math Value

Description

Returns MAD - median of the absolute values

Demo Code


import java.util.Arrays;
import java.util.Random;

public class Main{
    /**//from   w w w . ja  v a 2 s  .  c  om
     * 
     * Returns MAD - median of the absolute values
     * 
     * @param median
     * @param data
     * @return MAD
     */
    public static double getMAD(double median, double[] data) {
        double newarray[];
        double result;
        newarray = new double[data.length];
        for (int i = 0; i < data.length; i++) {
            newarray[i] = Math.abs(data[i] - median);
        }
        Arrays.sort(newarray);
        result = getMedian(newarray);
        return result;
    }
    /**
     *
     * Returns median
     * 
     * @param data
     * @return median
     */
    public static double getMedian(double[] data) {
        Arrays.sort(data);
        if (data.length % 2 == 0) {
            return (data[data.length / 2] + data[data.length / 2 - 1]) / 2;
        } else {
            return data[(data.length - 1) / 2];
        }
    }
}

Related Tutorials