Returns the sum of a given column of the matrix (the numbering starts from 1) - Java java.lang

Java examples for java.lang:Math Matrix

Description

Returns the sum of a given column of the matrix (the numbering starts from 1)

Demo Code


//package com.java2s;

public class Main {
    /** /*from  w w  w  .j a v  a  2  s  .co m*/
     * Returns the sum of a given column of the matrix (the numbering starts from 1)
     * @param matrix Matrix whose column sum is to be calculated
     * @param colNum Integer containing the column number (starting with 1)
     * @return Double containing the sum of the column
     */
    public static <T> double getColumnSum(T[][] matrix, int colNum) {

        double colSum = 0.0;
        colNum = colNum - 1;

        for (int i = 0; i < matrix.length; i++) {

            colSum = colSum + (Double) matrix[i][colNum];
        }

        return colSum;
    }
}

Related Tutorials