Java Array Average avg(int[][] values)

Here you can find the source of avg(int[][] values)

Description

avg

License

Open Source License

Declaration

public static double[] avg(int[][] values) 

Method Source Code

//package com.java2s;
/*/*from   www .j a v a2 s  .co m*/
 * ===========================================================
 * GTNA : Graph-Theoretic Network Analyzer
 * ===========================================================
 * 
 * (C) Copyright 2009-2011, by Benjamin Schiller (P2P, TU Darmstadt)
 * and Contributors
 * 
 * Project Info:  http://www.p2p.tu-darmstadt.de/research/gtna/
 * 
 * GTNA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GTNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * ---------------------------------------
 * Util.java
 * ---------------------------------------
 * (C) Copyright 2009-2011, by Benjamin Schiller (P2P, TU Darmstadt)
 * and Contributors 
 * 
 * Original Author: Benjamin Schiller;
 * Contributors:    -;
 * 
 * Changes since 2011-05-17
 * ---------------------------------------
 */

public class Main {
    public static double[] avg(int[][] values) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i].length > max) {
                max = values[i].length;
            }
        }
        double[] avg = new double[max];
        for (int i = 0; i < avg.length; i++) {
            for (int j = 0; j < values.length; j++) {
                if (values[j].length > i) {
                    avg[i] += values[j][i];
                }
            }
            avg[i] /= (double) values.length;
        }
        return avg;
    }

    public static double[] avg(double[][] values) {
        int max = 0;
        for (int i = 0; i < values.length; i++) {
            if (values[i].length > max) {
                max = values[i].length;
            }
        }
        double[] avg = new double[max];
        for (int i = 0; i < avg.length; i++) {
            for (int j = 0; j < values.length; j++) {
                if (values[j].length > i) {
                    avg[i] += values[j][i];
                }
            }
            avg[i] /= (double) values.length;
        }
        return avg;
    }

    public static double avg(int[] values) {
        int sum = 0;
        for (int i = 0; i < values.length; i++) {
            sum += values[i];
        }
        return (double) sum / (double) values.length;
    }

    public static double avg(double[] values) {
        double sum = 0;
        for (int i = 0; i < values.length; i++) {
            sum += values[i];
        }
        return sum / (double) values.length;
    }
}

Related

  1. avg(float... numbers)
  2. avg(int... values)
  3. avg(int[] arr)
  4. avg(int[] values)
  5. avg(int[] xs)
  6. avgarr(int[] a)
  7. avgArray(double[] values, int maxLength)
  8. avgArrayAndDouble(double[] array, double doublevalue)
  9. avgIgnoreNaN(double[] values)