Java mean means(double[][] input)

Here you can find the source of means(double[][] input)

Description

Return an array of the means of each column in the 2D input

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

public static double[] means(double[][] input) 

Method Source Code

//package com.java2s;
/*//  w  w  w  .j a  v a  2s  . c o  m
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program 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.
 *  
 *  This program 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/>.
 */

public class Main {
    /**
     * Return an array of the means of each column in the 2D input
     * 
     * @param input
     * @return
     */
    public static double[] means(double[][] input) {
        double[] theMeans = sums(input);
        for (int i = 0; i < theMeans.length; i++) {
            theMeans[i] = theMeans[i] / input.length;
        }
        return theMeans;
    }

    /**
     * Return an array of the means of each column in the 2D input
     * 
     * @param input
     * @param startRow which row to start from
     * @param length how many rows to take the mean over
     * @return
     */
    public static double[] means(double[][] input, int startRow, int length) {
        double[] theMeans = sums(input, startRow, length);
        for (int i = 0; i < theMeans.length; i++) {
            theMeans[i] = theMeans[i] / length;
        }
        return theMeans;
    }

    /**
     * Return an array of the sums for each column in the 2D input
     * 
     * @param input
     * @return
     */
    public static double[] sums(double[][] input) {
        double[] theSums = new double[input[0].length];
        for (int r = 0; r < input.length; r++) {
            for (int c = 0; c < input[r].length; c++) {
                theSums[c] += input[r][c];
            }
        }
        return theSums;
    }

    /**
     * Return an array of the sums for each column in the 2D input
     * 
     * @param input
     * @param startRow which row to start from
     * @param length how many rows to take the sum over
     * @return
     */
    public static double[] sums(double[][] input, int startRow, int length) {
        double[] theSums = new double[input[0].length];
        for (int r = startRow; r < startRow + length; r++) {
            for (int c = 0; c < input[r].length; c++) {
                theSums[c] += input[r][c];
            }
        }
        return theSums;
    }
}

Related

  1. meanFast(final double[] values)
  2. meanFilter(float[] weights, int context)
  3. meanGreenwichSideralTime(double t)
  4. meanImage(float[][]... images)
  5. meanLow(final int a, final int b)
  6. meanSlow(final double[] values)
  7. meanSml(final int a, final int b)
  8. meanSquare(float[] a, int off, int length)
  9. meanSquaredError(double[] x, double[] y)