Javascript Program to multiply two matrices
Last Updated :
26 Jul, 2024
Given two matrices, the task to multiply them. Matrices can either be square or rectangular.
Example:
Input : mat1[][] = {{2, 4}, {1, 4}}
mat2[][] = {{1, 4}, {1, 3}}
Output : {{6, 16}, {7, 18}}
Multiplication of Square Matrices :
The below program multiplies two square matrices of size 4*4, we can change N for different dimensions.
JavaScript
<script>
// Javascript program to multiply
// two square matrices.
const N = 4;
// This function multiplies
// mat1[][] and mat2[][], and
// stores the result in res[][]
function multiply(mat1, mat2, res)
{
let i, j, k;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
res[i][j] = 0;
for (k = 0; k < N; k++)
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
// Driver Code
let i, j;
// To store result
let res = new Array(N);
for (let k = 0; k < N; k++)
res[k] = new Array(N);
let mat1 = [ [ 1, 1, 1, 1 ],
[ 2, 2, 2, 2 ],
[ 3, 3, 3, 3 ],
[ 4, 4, 4, 4 ] ];
let mat2 = [ [ 1, 1, 1, 1 ],
[ 2, 2, 2, 2 ],
[ 3, 3, 3, 3 ],
[ 4, 4, 4, 4 ] ];
multiply(mat1, mat2, res);
document.write("Result matrix is <br>");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
document.write(res[i][j] + " ");
document.write("<br>");
}
</script>
OutputResult matrix is
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40
Time complexity: O(n3). It can be optimized using Strassen’s Matrix Multiplication
Auxiliary Space: O(n2)
Multiplication of Rectangular Matrices :
We use pointers in C to multiply to matrices. Please refer to the following post as a prerequisite of the code.
How to pass a 2D array as a parameter in C?
JavaScript
<script>
// Javascript program to multiply two
// rectangular matrices
// Multiplies two matrices mat1[][]
// and mat2[][] and prints result.
// (m1) x (m2) and (n1) x (n2) are
// dimensions of given matrices.
function multiply(m1, m2, mat1, n1, n2, mat2)
{
let x, i, j;
let res = new Array(m1);
for (i = 0; i < m1; i++)
res[i] = new Array(n2);
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
res[i][j] = 0;
for (x = 0; x < m2; x++)
{
res[i][j] += mat1[i][x] * mat2[x][j];
}
}
}
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
document.write(res[i][j] + " ");
}
document.write("<br>");
}
}
// Driver code
let mat1 = [ [ 2, 4 ], [ 3, 4 ] ];
let mat2 = [ [ 1, 2 ], [ 1, 3 ] ];
let m1 = 2, m2 = 2, n1 = 2, n2 = 2;
// Function call
multiply(m1, m2, mat1, n1, n2, mat2);
</script>
Time complexity: O(n3). It can be optimized using Strassen’s Matrix Multiplication
Auxiliary Space: O(m1 * n2)
Please refer complete article on Program to multiply two matrices for more details!
Input : mat1[][] = {{1, 2}, {3, 4}}
mat2[][] = {{1, 1}, {1, 1}}
Output : {{3, 3}, {7, 7}}
Similar Reads
Program to multiply two matrices Given two matrices, the task is to multiply them. Matrices can either be square or rectangular:Examples: (Square Matrix Multiplication)Input: m1[m][n] = { {1, 1}, {2, 2} }m2[n][p] = { {1, 1}, {2, 2} }Output: res[m][p] = { {3, 3}, {6, 6} }(Rectangular Matrix Multiplication)Input: m1[3][2] = { {1, 1},
7 min read
JavaScript Program to Add Two Matrices Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices In this article, we will see how we can perform the addition of two input matrices using JavaScript. Example: Table of ContentUsing Loop in J
3 min read
Python Program to multiply two matrices Given two matrices, the task to multiply them. Matrices can either be square or rectangular. Examples: Input : mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{1, 1}, {1, 1}} Output : {{3, 3}, {7, 7}} Input : mat1[][] = {{2, 4}, {3, 4}} mat2[][] = {{1, 2}, {1, 3}} Output : {{6, 16}, {7, 18}}Recommended: Ple
3 min read
Javascript Program for Kronecker Product of two matrices Given a {m} imes{n} matrix A and a {p} imes{q} matrix B, their Kronecker product C = A tensor B, also called their matrix direct product, is an {(mp)} imes{(nq)} matrix. A tensor B = |a11B a12B| |a21B a22B|= |a11b11 a11b12 a12b11 a12b12| |a11b21 a11b22 a12b21 a12b22| |a11b31 a11b32 a12b31 a12b32| |a
3 min read
Program to multiply two Matrix by taking data from user Given two matrices, the task is to multiply them. The size and number of elements of matrices are to be read from the keyboard. Examples:Â Â Input: row1 = 2, col1 = 2, mat1[][] = {{1, 2}, {3, 4}}, row2 = 2, col2 = 2, mat2[][] = {{1, 1}, {1, 1}} Output: {{3, 3}, {7, 7}} Input: row1 = 2, col1 = 2, mat1
15 min read
Program for addition of two matrices Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices. Approach: Below is the idea to solve the problem.Iterate over every cell of matrix (i, j), add the corresponding values of the two matrices
5 min read
Program for subtraction of matrices Given two m x n matrices m1 and m2, the task is to subtract m2 from m1 and return res.Input: m1 = {{1, 2}, {3, 4}}, m2 = {{4, 3}, {2, 1}}Output: {{-3, -1}, {1, 3}}Input: m1 = {{3, 3, 3}, {3, 3, 3}}, m1 = {{2, 2, 2}, {1, 1, 1}},Output: {{1, 1, 1}, {2, 2, 2}},We traverse both matrices element by eleme
5 min read
Program to concatenate two given Matrices of same size Given two matrices A and B of size M x N, the task is to concatenate them into one matrix.Concatenation of matrix: The process of appending elements of every row of the matrix one after the other is known as the Concatenation of matrix. Examples: Input: A[][] = {{1, 2}, {3, 4}}, B[][] = {{5, 6}, {7,
6 min read
Program for scalar multiplication of a matrix Given a 2D matrix mat[][] with n rows and m columns and a scalar element k, the task is to find out the scalar product of the given matrix.Examples: Input: mat[][] = [[2, 3], [5, 4]]k = 5Output: [[10, 15], [25, 20]]Input:mat[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]k = 4Output: [[4, 8, 12], [16, 20, 2
4 min read
Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3
3 min read