Home  >  Article  >  Web Front-end  >  JavaScript program: find the largest element of each row in a matrix

JavaScript program: find the largest element of each row in a matrix

王林
王林forward
2023-08-25 16:05:22666browse

JavaScript program: find the largest element of each row in a matrix

We will write a program that uses the future continuous tense to find the maximum element of each row in a given matrix. This would involve looping through each row of the matrix and comparing the elements to find the maximum value.

To implement this solution, we will use built-in functions like Math.max() and loops to iterate through the matrix and get the desired result. At the end of our program we will have a solution that can handle matrices of any size and provide the maximum value per row in an efficient and effective manner.

method

This is a brief explanation of how to find the maximum element of each row of a matrix using JavaScript−

  • Define a matrix with multiple rows and columns.

  • Loop through each row of the matrix.

  • In each row, loop through each column and record the largest element found so far.

  • After looping through all columns, the current largest element will become the largest element of the current row.

  • Store the maximum element of each row in an array.

  • After looping through all rows, the array will contain the largest element of each row.

  • Return an array as the result.

The translation of

Example

is:

Example

This is a complete JavaScript program example that can find the largest element of each row in a matrix −

function findMaxOfRows(matrix) {
   let result = [];
   for (let i = 0; i < matrix.length; i++) {
      let max = -Infinity;
      for (let j = 0; j < matrix[i].length; j++) {
         if (matrix[i][j] > max) {
            max = matrix[i][j];
         }
      }
      result.push(max);
   }
   return result;
}
const matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
console.log(findMaxOfRows(matrix)); 
The Chinese translation of

Explanation

is:

Explanation

  • Function findMaxOfRows Accepts a two-dimensional matrix as a parameter.

  • Variable result is used to store the maximum element of each row.

  • Outer layerfor Loop through each row of the matrix.

  • Inner for Loops through each element of the current row.

  • The if statement checks whether the current element is greater than the current maximum value. If so, update the maximum value using the current element.

  • After the inner for loop, add the maximum value of the current row to the result array.

  • OuterforThe loop continues execution until all rows have been processed.

  • Finally, return the result array containing the largest element of each row.

  • Pass the sample matrix as a parameter to the findMaxOfRows function and log the results in the console.

The above is the detailed content of JavaScript program: find the largest element of each row in a matrix. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete