JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Perform Matrix Addition
Photo Credit to CodeToFun
π Introduction
Matrix addition is a fundamental operation in linear algebra and computer science. It involves adding corresponding elements of two matrices to create a new matrix.
This operation is essential in various fields, including computer graphics, scientific computing, and data analysis.
In this tutorial, we'll explore a JavaScript program that performs matrix addition.
Understanding matrix operations and implementing them in C can provide a solid foundation for more complex computations.
π Example
Let's take a look at the JavaScript code that accomplishes matrix addition.
// Function to perform matrix addition
function addMatrices(matrix1, matrix2) {
const resultMatrix = [];
for (let i = 0; i < matrix1.length; i++) {
const row = [];
for (let j = 0; j < matrix1[i].length; j++) {
row.push(matrix1[i][j] + matrix2[i][j]);
}
resultMatrix.push(row);
}
return resultMatrix;
}
// Function to display a matrix
function displayMatrix(matrix) {
for (let i = 0; i < matrix.length; i++) {
console.log(matrix[i].join(' '));
}
}
// Sample matrices for testing
const matrix1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
const matrix2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
];
// Perform matrix addition
const resultMatrix = addMatrices(matrix1, matrix2);
// Displaying matrices and result
console.log("Matrix 1:");
displayMatrix(matrix1);
console.log("\nMatrix 2:");
displayMatrix(matrix2);
console.log("\nResultant Matrix:");
displayMatrix(resultMatrix);
π» Testing the Program
Feel free to replace the sample matrices with your own 3x3 matrices to test the program with different input.
Matrix 1: 1 2 3 4 5 6 7 8 9 Matrix 2: 9 8 7 6 5 4 3 2 1 Resultant Matrix: 10 10 10 10 10 10 10 10 10
Run the script in a JavaScript environment (such as a browser console or Node.js) to see the result of the matrix addition.
π§ How the Program Works
- The program defines a function addMatrices to add two matrices and a function displayMatrix to display a matrix.
- Sample matrices matrix1 and matrix2 are provided for testing.
- The addMatrices function is called to perform the addition, and the result is stored in resultMatrix.
- The matrices and the result are displayed using the displayMatrix function.
π§ Understanding the Concept of Matrix Addition
Matrix addition involves adding corresponding elements of two matrices to create a new matrix.
Given two matrices A and B, the sum C is calculated as follows:
Cij = Aij + Bij.
where Cij is the element at the i-th row and j-th column of matrix C, and Aij and Bij are the corresponding elements of matrices A and B.
Matrix addition is only defined for matrices of the same size; that is, they must have the same number of rows and columns.
π’ Optimizing the Program
While the provided program is effective, there are optimizations that can be applied for larger matrices. Consider exploring techniques such as parallelization for improved performance.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Perform Matrix Addition), please comment here. I will help you immediately.