JavaScript Program to Add Two Matrices
Last Updated :
26 Oct, 2023
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:

Approach 1: Using Loop in JavaScript
In this approach, we are using for loops to iterate over every individual element of the input matrices. This approach can perform the addition of any of the number of matrices, and then we are performing the addition of the response element and saving the result of the addition in the result matrix.
Syntax:
for (statement 1; statement 2; statement 3) {
code here...
}
Example: In this example, we will be performing the Addition of two Matrices in JavaScript by using Loops.
JavaScript
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],
];
let resmat = [];
for (let i = 0; i < mat1.length; i++) {
let r = "";
for (let j = 0; j < mat1[i].length; j++) {
r += mat1[i][j] + mat2[i][j] + " ";
}
resmat.push(r.trim());
}
resmat.forEach(r => console.log(r));
Output2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
Approach 2: Using map() method
In this approach, we are using the inbuilt 'map' method to create a new output matrix by adding the input matrices. Here, the code can perform the addition of two matrices in JavaScript.
Syntax:
map((element, index, array) => { /* … */ })
Example: In this example, we will be performing the Addition of two Matrices in JavaScript by using map method.
JavaScript
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],
];
let resmat = mat1.map((r, i) =>
r.map((v, j) => v + mat2[i][j])
);
for (let i = 0; i < resmat.length; i++) {
console.log(resmat[i].join(' '));
}
Output2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
Approach 3: Using the Array.from() method
In this approach, we are using the Array.from() method in JavaScript. By using this function, we create a new matrix by iterating over each row and column of all the input matrices. Before this, we are flattening the input matrices to convert the 2D array into the 1D array so that the element additon will be easily performed.
Syntax:
Array.from(object, mapFunction, thisValue)
Example: In this example, we will be performing the Addition of two Matrices in JavaScript by using Array.from() method.
JavaScript
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],
];
let m = Array.from(mat1.flat());
let n = Array.from(mat2.flat());
let resarr = m.map((v, i) => v + n[i]);
let resmat = [];
while (resarr.length) {
resmat.push(resarr.splice(0, mat1[0].length));
}
resmat.forEach(row =>
console.log(row.join(' ')));
Output2 2 2 2
4 4 4 4
6 6 6 6
8 8 8 8
Similar Reads
Javascript Program to multiply two matrices 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 ch
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
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
Operations on Matrices in R In R, matrices are two-dimensional arrays of data numeric, logical or complex organized in rows and columns. Matrices are used to depict the data in a structured and well-organized format. It is necessary to enclose the elements of a matrix in parentheses or brackets.A matrix is defined by its order
6 min read
How to Perform Matrix Operations using Math.js? Matrix operations can be computed in JavaScript using the math.js library, which provides a range of functions for handling matrices. With math.js, you can perform basic operations such as addition, subtraction, and multiplication, as well as more advanced operations like transposition, inversion, a
3 min read
Append a row to a matrix using R In this article, we will examine various methods to append a row into a matrix in the R Programming Language. What is a matrix?A matrix is a two-dimensional data set, which is a collection of rows and columns. It contains n rows and m columns. Inside the matrix, rows are arranged horizontally and co
4 min read