Home > Web Front-end > JS Tutorial > JavaScript program to efficiently calculate the sum of matrix diagonals

JavaScript program to efficiently calculate the sum of matrix diagonals

王林
Release: 2023-09-18 09:19:35
forward
1380 people have browsed it

JavaScript 程序可有效计算矩阵对角线之和

We will write a program in JavaScript to efficiently calculate the sum of the diagonals of a matrix. To do this, we will use a loop structure to iterate over the matrix and add elements located at positions corresponding to the diagonals. By exploiting the mathematical properties of matrices, we can minimize the amount of calculations required to sum the diagonals. With this approach we will be able to handle matrices of various sizes in a computationally efficient manner.

method

  • To calculate the sum of the diagonals of a matrix, we need to add the element values ​​on the main diagonal (top left to bottom right) and the secondary diagonal (top right to bottom - left)

  • You can use a double loop approach where one loop goes through the rows and the second loop goes through the columns to access the elements on the diagonal.

  • We can keep two variables to store the sum of elements on the main diagonal and sub-diagonal respectively.

  • To access elements on the main diagonal, we need to add the current row index and column index, while for elements on the sub-diagonal, we need to subtract the column index from the row index.

    李>
  • Finally, we return the sum of the two variables as the result, which will give the sum of the elements on both diagonals of the matrix.

Example

This is an example of a JavaScript program that efficiently calculates the sum of the diagonals of a matrix -

function diagonalSum(matrix) {
   let sum = 0;
   let n = matrix.length;
    
   for (let i = 0; i < n; i++) {
      sum += matrix[i][i];
      sum += matrix[i][n - i - 1];
   }
     
   if (n % 2 !== 0) {
      let mid = Math.floor(n / 2);
      sum -= matrix[mid][mid];
   }
     
   return sum;
}
const matrix = [[1, 2, 3],[4, 5, 6], [7, 8, 9]];
console.log(diagonalSum(matrix));
Copy after login

illustrate

  • Initialize the variable sum to store the sum of the diagonals, and initialize the variable n to store the number of rows in the matrix.

  • Use a for loop to iterate over the matrix, adding the diagonal values ​​to sum. For each iteration i, we add the main diagonal matrix[ i][i] and the antidiagonal matrix[i][n - i - 1].

  • If the number of rows of the matrix is ​​odd, we subtract the middle value matrix[mid][mid] (where mid is the middle row index, use Math.floor(n / 2)) is calculated because it will be added twice.

  • Returns the value of the sum.

The time complexity of this algorithm is O(n), making it an efficient solution for computing the sum of the diagonals of a matrix.

The above is the detailed content of JavaScript program to efficiently calculate the sum of matrix diagonals. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template