Java Matrix Sum sum(int[][][] X, int[] coords)

Here you can find the source of sum(int[][][] X, int[] coords)

Description

sum

License

Open Source License

Declaration

private static int sum(int[][][] X, int[] coords) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static int sum(int[][][] X, int[] coords) {
        int x = coords[0];
        int y = coords[1];
        int z = coords[2];

        int xlow = x == -1 ? 0 : x;
        int xhigh = x == -1 ? X.length - 1 : x;
        int ylow = y == -1 ? 0 : y;
        int yhigh = y == -1 ? X[0].length - 1 : y;
        int zlow = z == -1 ? 0 : z;
        int zhigh = z == -1 ? X[0][0].length - 1 : z;

        int sum = 0;
        int count = 0;

        for (int i = xlow; i <= xhigh; i++) {
            for (int j = ylow; j <= yhigh; j++) {
                for (int m = zlow; m <= zhigh; m++) {
                    int c = X[i][j][m];

                    if (c >= 0) {
                        sum += c;/*  w w w .j av a  2s  . c  o  m*/
                        count++;
                    }
                }
            }
        }

        return count == 0 ? -1 : sum;
    }
}

Related

  1. sum(double[][] kernel1, double[][] kernel2)
  2. sum(double[][] o)
  3. sum(double[][] X, int axis)
  4. sum(float[][] a1, float[][] a2)
  5. sum(int M[][])
  6. sumArrayDim(double[][] array, int dimToSummarize)
  7. sumArrays(double a[][], double b[][], String sign)
  8. sumAxis1(double[][] X)
  9. sumCols(boolean[][] inputMatrix)