Java Array Average average1(double[] d)

Here you can find the source of average1(double[] d)

Description

average

License

Open Source License

Parameter

Parameter Description
d a parameter

Declaration

public static double average1(double[] d) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  w w w  .  ja v a2s  .co m*/
     * 
     * @param d
     * @return
     */
    public static double average1(double[] d) { // Schneller
        if (d == null || d.length < 1)
            return Double.NaN;
        double retVal = 0;

        int countNonNAN = 0;
        for (int i = 0; i < d.length; i++) {
            if (Double.isNaN(d[i]) || Double.isInfinite(d[i]))
                continue;
            countNonNAN++;
            retVal += d[i];
        }

        if (countNonNAN <= 0)
            return Double.NaN;
        return (retVal / countNonNAN);
    }

    /**
     * 
     * @param d
     * @return
     */
    public static double[] average1(double[][] d) { // Schneller
        if (d.length < 1)
            return new double[0];
        double[] retVal = null;

        int countNonNull = 0;
        for (int i = 0; i < d.length; i++) {
            if (d[i] == null)
                continue; // kommt vor wenn er sequenz i nicht mappen kann
            countNonNull++;
            if (retVal == null)
                retVal = new double[d[i].length];
            for (int j = 0; j < d[i].length; j++)
                retVal[j] += d[i][j];
        }

        if (retVal == null)
            return null; // Koomt vor wenn er alle sequenzen nicht mappen kann
        for (int i = 0; i < retVal.length; i++)
            retVal[i] /= countNonNull;

        return retVal;
    }
}

Related

  1. average(int[] pixels)
  2. average(int[] x)
  3. average(long[] array)
  4. average(long[] vals)
  5. average(Number[] numbers)
  6. average2(double[] d)
  7. average4RGBA(int var0, int var1, int var2, int var3)
  8. averageChroma(float[] chromaSums, int countChromas)
  9. averageLuminance(double[] luminance)