简体   繁体   English

二维数组的总和

[英]Sum in 2-dimensional array

I have a matrix : 我有一个矩阵:

matrix = [[0, 1, 1, 2],
          [0, 5, 0, 0],
          [2, 0, 3, 3]]

I need to calculate Sum of all array elements which are not under 0 So, in this example Sum should be = 9 我需要计算不小于0的所有数组元素的总和,因此,在此示例中,总和应为= 9

I have this function: 我有这个功能:

function matrixElementsSum(matrix) {
  // Write your code here
 var Summa =0
  for (i = 0; i<4; i++){
      var sum =0;
    // console.log(matrix[i].length); //4
    for(j=0; j<matrix.length; j++){
      console.log("Matrix JI "+ matrix[j][i])
         sum = sum +matrix[j][i]; 
         if (matrix[j][i-1]!=0){
           console.log(matrix[j][i-1])
           Summa =Summa+ sum;
         }
    }
    console.log('-----------' +Summa)
    console.log("Sum "+sum);
  }

  return Summa;
}

i think i need to change if (matrix[j-1][i]!=0) but it doesn't work 我想我需要更改if (matrix[j-1][i]!=0)但是它不起作用

You can use reduce() and inside forEach() loop for this. 您可以为此使用reduce()forEach()循环内部。 If the current element in foreach loop is zero then you can store index of that element in one other object zero and you can use that object to check if there was zero with same index. 如果foreach循环中的当前元素为零,则可以将该元素的索引存储在另一个对象zero然后可以使用该对象检查索引是否为零。

 var matrix = [ [0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3] ] var zero = {} var sum = matrix.reduce(function(r, e, i) { e.forEach(function(n, j) { if (n == 0) zero[j] = true; if (!zero[j]) r += n; }) return r; }, 0) console.log(sum) 

You can sum 2 arrays and ignore numbers from the bottom array, which items from the same index on the top array are 0. 您可以对2个数组求和,而忽略底部数组中的数字,顶部数组中相同索引的项为0。

Now you can iterate the matrix from the end, and sum the resulting array. 现在,您可以从末尾迭代矩阵,并对结果数组求和。

 const matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]; const sumNotUnderZero = (bottom, top) => top.map((v, i) => v ? v + bottom[i] : v); const result = matrix.reduceRight(sumNotUnderZero) .reduce((s, n) => s + n); console.log(result); 

You could use Array#reduceRight for building another array with valued column sums and then use Array#reduce for a single number. 您可以使用Array#reduceRight构建具有值列总和的另一个数组,然后对单个数字使用Array#reduce

 var matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]], result = matrix.reduceRight(function (a, b) { return b.map(function (c, i) { return c && c + a[i]; }); }).reduce(function (a, b) { return a + b; }); console.log(result); 

Should be able to simplify it and use this: 应该能够简化并使用它:

function matrixElementsSum(matrix) {
    var Summa =0
    for (i = 0; i < matrix.length; i++)
      for(j = 0; j < matrix[i].length; j++)
         if (matrix[i-1][j] != 0)
           Summa = Summa + matrix[i][j];

    return Summa;
}

You need to access first the array above your current one, hence the matrix[i-1] and then the same column, hence the [j] in (matrix[i-1])[j] ~ matrix[i-1][j] 需要访问上述当前的一个第一阵列,因此matrix[i-1]然后在同一列中,因此, [j](matrix[i-1])[j] ~ matrix[i-1][j]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM