JavaScript Program to find Matrix Multiplication
You are Here:
Find Matrix Multiplication
In the following example, we will multiply the two given matrices (two-dimensional arrays).
Note: In Javascript, you have to initialize the two dimensional array before using it, because it is technically impossible to create a 2d array in javascript.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<h1>JS Matrix Multiplication</h1>
<script>
var i, j, k;
var arr1 = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]
];
var arr2 = [
[2, 2, 2],
[2, 2, 2],
[2, 2, 2]
];
var arr3 =[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
document.write("Matrix A (3 x 3):<br>");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
document.write(arr1[i][j] +" ");
document.write("<br>");
}
document.write("Matrix B (3 x 3):<br>");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
document.write(arr2[i][j] +" ");
document.write("<br>");
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
for(k=0; k<3; k++)
arr3[i][j] = arr3[i][j] + arr1[i][k] * arr2[k][j];
}
}
document.write("Output:<br>");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
document.write(arr3[i][j]+" ");
document.write("<br>");
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.