Java Matrix Sum sums(double[][] input)

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

Description

Return an array of the sums for each column in the 2D input

License

Open Source License

Parameter

Parameter Description
input a parameter

Declaration

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

Method Source Code

//package com.java2s;
/*//from w w w . j  av a  2 s . c om
 *  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 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. sumLines(double a[][])
  2. sumMetrix(double[][] total, double[][] element)
  3. sumOfMatrixElements(double[][] m)
  4. sumRow(int[][] matrix, int u)
  5. sumRow(int[][] table, int column)
  6. sumSpecificIndices(double[] input, int[][] indices, int columnInIndices)
  7. sumSq(float[][] arr)