
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Take in two 2-D arrays of numbers and returns their matrix multiplication result- JavaScript
We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.
Let’s say the following are our two matrices −
// 5 x 4 let a = [ [1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1], [1, 1, 1, 1], [5, 7, 2, 6] ]; // 4 x 6 let b = [ [1, 4, 7, 3, 4, 6], [2, 5, 8, 7, 3, 2], [3, 6, 9, 6, 7, 8], [1, 1, 1, 2, 3, 6] ];
Example
Let’s write the code for this function −
const multiplyMatrices = (a, b) => { if (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) { throw new Error('arguments should be in 2-dimensional array format'); } let x = a.length, z = a[0].length, y = b[0].length; if (b.length !== z) { // XxZ & ZxY => XxY throw new Error('number of columns in the first matrix should be the same as the number of rows in the second'); } let productRow = Array.apply(null, new Array(y)).map(Number.prototype.valueOf, 0); let product = new Array(x); for (let p = 0; p < x; p++) { product[p] = productRow.slice(); } for (let i = 0; i < x; i++) { for (let j = 0; j < y; j++) { for (let k = 0; k < z; k++) { product[i][j] += a[i][k] * b[k][j]; } } } return product; } // 5 x 4 let a = [ [1, 2, 3, 1], [4, 5, 6, 1], [7, 8, 9, 1], [1, 1, 1, 1], [5, 7, 2, 6] ]; // 4 x 6 let b = [ [1, 4, 7, 3, 4, 6], [2, 5, 8, 7, 3, 2], [3, 6, 9, 6, 7, 8], [1, 1, 1, 2, 3, 6] ]; // should result in a 5 x 6 matrix console.log(multiplyMatrices(a, b));
Output
This will produce the following output on console −
[ [ 15, 33, 51, 37, 34, 40 ], [ 33, 78, 123, 85, 76, 88 ], [ 51, 123, 195, 133, 118, 136 ], [ 7, 16, 25, 18, 17, 22 ], [ 31, 73, 115, 88, 73, 96 ] ]
- Related Articles
- Equality of two 2-D arrays - JavaScript
- Checking for the similarity of two 2-D arrays in JavaScript
- Python program multiplication of two matrix.
- Algorithm for matrix multiplication in JavaScript
- Matrix product of two arrays in Numpy
- Take two numbers m and n & return two numbers whose sum is n and product m in JavaScript
- Column sum of elements of 2-D arrays in JavaScript
- How to subtract elements of two arrays and store the result as a positive array in JavaScript?
- Splitting array of numbers into two arrays with same average in JavaScript
- How to calculate GCD of two or more numbers/arrays in JavaScript?
- Print the corner elements and their sum in a 2-D matrix in C Program.
- Rotating a 2-D (transposing a matrix) in JavaScript
- C++ Program to Implement Booth’s Multiplication Algorithm for Multiplication of 2 signed Numbers
- isSubset of two arrays in JavaScript
- Matrix Multiplication and Normalization in C program

Advertisements