Returns the sum of a given row 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 row of the matrix (the numbering starts from 1)

Demo Code


//package com.java2s;

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

        double rowSum = 0.0;
        rowNum = rowNum - 1;

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

            rowSum = rowSum + (Double) matrix[rowNum][i];
        }

        return rowSum;
    }
}

Related Tutorials