Java mean mean(final double[] data, final boolean noNaN)

Here you can find the source of mean(final double[] data, final boolean noNaN)

Description

Calc the mean of an array of double

License

GNU General Public License

Parameter

Parameter Description
data Data
noNaN true if NaN value must be removed from the computation

Return

the mean or NaN if the data is null

Declaration

public static double mean(final double[] data, final boolean noNaN) 

Method Source Code

//package com.java2s;
/*//from  www . j a  v  a 2s. co  m
 *                      Nividic development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the microarray platform
 * of the ?cole Normale Sup?rieure and the individual authors.
 * These should be listed in @author doc comments.
 *
 * For more information on the Nividic project and its aims,
 * or to join the Nividic mailing list, visit the home page
 * at:
 *
 *      http://www.transcriptome.ens.fr/nividic
 *
 */

public class Main {
    /**
     * Calc the mean of an array of double
     * @param data Data
     * @param noNaN true if NaN value must be removed from the computation
     * @return the mean or NaN if the data is null
     */
    public static double mean(final double[] data, final boolean noNaN) {

        return mean(noNaN ? removeNaN(data) : data);
    }

    /**
     * Calc the mean of an array of double
     * @param data Data
     * @return the mean or NaN if the data is null
     */
    public static double mean(final double[] data) {

        if (data == null)
            return Double.NaN;

        int count = 0;
        float sum = 0;

        for (int i = 0; i < data.length; i++) {

            count++;
            sum += data[i];
        }

        return sum / count;
    }

    /**
     * Remove NaN from an array
     * @param data Array to use
     * @return an new array without NaN values
     */
    public static double[] removeNaN(final double[] data) {

        if (data == null)
            return null;

        int count = 0;

        for (int i = 0; i < data.length; i++)
            if (Double.isNaN(data[i]))
                count++;

        final double[] result = new double[count];
        count = 0;

        for (int i = 0; i < result.length; i++)
            if (!Double.isNaN(data[i]))
                result[count++] = data[i];

        return result;
    }
}

Related

  1. mean(double[][] image)
  2. mean(double[][] input, int column)
  3. mean(double[][] matrix)
  4. mean(double[][] o)
  5. mean(final double[] a)
  6. mean(final double[] expected, final int begin, final int end)
  7. mean(final double[] in, final int start, final int stop)
  8. mean(final double[] values)
  9. mean(final double[] vec)