54.1k views
2 votes
Write a JavaScript to multiple 2 square matrices (3,3). You can use any numbers as elements of the matrix.

User Tiberius
by
5.1k points

1 Answer

0 votes

Answer:

Here is the JavaScript program:

function MatricesMul(matrix1, matrix2) {

var product = [];

for (var i = 0; i < matrix1.length; i++) {

product[i] = [];

for (var j = 0; j < matrix2[0].length; j++) {

var total = 0;

for (var k = 0; k < matrix1[0].length; k++) {

total += matrix1[i][k] * matrix2[k][j]; }

product[i][j] = total; } }

return product;}

var matrix1 = [[1,2,3],[4,5,6],[7,8,9]]

var matrix2 = [[3,2,1],[6,5,4],[1,2,3]]

var output = MatricesMul(matrix1, matrix2)

document.write(output);

Step-by-step explanation:

The function MatricesMul(matrix1, matrix2) takes two matrices (matrix1 and matrix2) as parameters.

var product = []; product works as an array to contain the result of the multiplication of the two matrices matrix1 and matrix2.

for (var i = 0; i < matrix1.length; i++) this is a loop which iterates through the matrix1 and it continues to execute until the loop variable i exceeds the length of the matrix1. This is basically the loop which iterates through the rows of matrix1.

for (var j = 0; j < matrix2[0].length; j++) this is a loop which iterates through matrix2 and it continues to execute untill the loop variable j exceeds the length of the element of matrix2 at 0-th index (first element). This is basically the loop which iterates through the colulmns of matrix2.

var total = 0; variable total is initialized to 0 and this variable is used to compute the sum.

for (var k = 0; k < matrix1[0].length; k++) this loop is used for the multiplication of the two matrices matrix1 and matrix2.

total += matrix1[i][k] * matrix2[k][j]; this statement first multiplies each element of matrix1 row to each element of the matrix2 column, takes the sum of pairwise products.

return product; returns the result of the multiplication of matrix1 and matrix2.

For example let matrix1 = [[1,2,3],[4,5,6],[7,8,9]] and matrix2 = [[3,2,1],[6,5,4],[1,2,3]] .

The first for loop moves through each row of matrix1. The first element of first row of matrix1 is 1, the second element is 2 and third is 3. The second loop iterates through each column of matrix2. Lets take the first column matrix2. The first element of first column of matrix2 i.e. 3, the second element is 6 and the third is 1. Third loop performs the pairwise multiplication of row of matrix1 to column of matrix2 and takes the sum of pairwise product. This means 1 is multiplied to 3, 2 is multiplied with 6 and 3 is multiplied to 1. This will take the form: 1*3 + 2*6 + 3*1 = 3 + 12 + 3 Now their sum is taken and the result is stored in total variable. So the sum of 3 + 12 + 3 is 18. This process will continue until all the rows elements of matrix1 and elements in all columns of matrix2 are multiplied and the sum of each pairwise product is taken.

var matrix1 = [[1,2,3],[4,5,6],[7,8,9]]

var matrix2 = [[3,2,1],[6,5,4],[1,2,3]]

The above two statements two (3,3) square matrices are given values.

var output = MatricesMul() this calls the MatricesMul method and passes the two matrices (matrix1, matrix2) to the function and stores the result of this multiplication to output variable.

document.write(output); displays the result of the multiplication of two square matrices .

Write a JavaScript to multiple 2 square matrices (3,3). You can use any numbers as-example-1
User Stanga
by
4.4k points