Here you can find the source of average2(double[] d)
Parameter | Description |
---|---|
d | a parameter |
public static double average2(double[] d)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; public class Main { /**/*ww w . j a v a2 s . c o m*/ * * @param d * @return */ public static double average2(double[] d) { // Keine to-large-numbers if (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[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1); retVal = retVal * (countNonNAN - 1) / (countNonNAN) + d[i] * 1 / (countNonNAN); } // Wenn irgendwo nur NaNs waren, das auch so wiedergeben if (countNonNAN <= 0) return Double.NaN; return retVal; } /** * * @param d * @return */ public static double[] average2(double[][] d) { // Keine to-large-numbers if (d.length < 1) return new double[0]; double[] retVal = null; ArrayList<Integer> spaltenCounter = new ArrayList<Integer>(); for (int i = 0; i < d.length; i++) { if (d[i] == null) continue; // kommt vor wenn er sequenz i nicht mappen kann if (retVal == null) retVal = new double[d[i].length]; for (int j = 0; j < d[i].length; j++) { if (spaltenCounter.size() <= j) spaltenCounter.add(0); if (Double.isNaN(d[i][j])) continue; // Deshalb auch der Spaltencounter: Skip NaN // eintr?ge. // retVal[j]=retVal[j] * i/(i+1) + d[i][j] * 1/(i+1); retVal[j] = retVal[j] * spaltenCounter.get(j) / (spaltenCounter.get(j) + 1) + d[i][j] * 1 / (spaltenCounter.get(j) + 1); spaltenCounter.set(j, spaltenCounter.get(j) + 1); } } // Wenn irgendwo nur NaNs waren, das auch so wiedergeben for (int i = 0; i < spaltenCounter.size(); i++) if (spaltenCounter.get(i) == 0) retVal[i] = Double.NaN; return retVal; } }