Rule of Multiplication: The number of columns in Matrix A must equal the number of rows in Matrix B.
Matrix A Dimensions
Matrix B Dimensions
Matrix A
×
Matrix B
Result Matrix (C)
Understanding Matrix Multiplication
Matrix multiplication is a fundamental operation in linear algebra. Unlike regular multiplication, multiplying two matrices involves taking the "dot product" of rows from the first matrix and columns from the second matrix.
Requirements for Multiplication
To multiply Matrix A by Matrix B, the number of columns in Matrix A must be equal to the number of rows in Matrix B. If Matrix A is an m × n matrix and Matrix B is an n × p matrix, their product Matrix C will be an m × p matrix.
How to Calculate the Product
The element in the $i^{th}$ row and $j^{th}$ column of the result matrix is calculated by multiplying the elements of the $i^{th}$ row of Matrix A by the corresponding elements of the $j^{th}$ column of Matrix B and summing the results:
Cij = (Ai1 × B1j) + (Ai2 × B2j) + … + (Ain × Bnj)
Example Calculation
Consider two 2×2 matrices:
Matrix A: [1, 2] [3, 4] Matrix B: [5, 6] [7, 8]
Top Left: (1×5) + (2×7) = 5 + 14 = 19
Top Right: (1×6) + (2×8) = 6 + 16 = 22
Bottom Left: (3×5) + (4×7) = 15 + 28 = 43
Bottom Right: (3×6) + (4×8) = 18 + 32 = 50
Result: [19, 22] [43, 50]
Why is this useful?
Matrix multiplication is used extensively in computer graphics for rotating and scaling 3D objects, in economics for modeling supply chains, in physics for quantum mechanics, and in data science for training neural networks.
function generateGrids() {
var rA = parseInt(document.getElementById('rowsA').value);
var cA = parseInt(document.getElementById('colsA').value);
var rB = parseInt(document.getElementById('rowsB').value);
var cB = parseInt(document.getElementById('colsB').value);
var errorDiv = document.getElementById('error-message');
var inputSection = document.getElementById('matrix-inputs-section');
var resultSection = document.getElementById('result-section');
errorDiv.style.display = 'none';
resultSection.style.display = 'none';
if (cA !== rB) {
errorDiv.innerHTML = "Error: Matrix A columns (" + cA + ") must equal Matrix B rows (" + rB + ").";
errorDiv.style.display = 'block';
inputSection.style.display = 'none';
return;
}
if (rA < 1 || cA < 1 || rB < 1 || cB 10 || cA > 10 || rB > 10 || cB > 10) {
errorDiv.innerHTML = "Please enter dimensions between 1 and 10.";
errorDiv.style.display = 'block';
return;
}
createTable('gridA', rA, cA, 'a');
createTable('gridB', rB, cB, 'b');
inputSection.style.display = 'block';
}
function createTable(containerId, rows, cols, prefix) {
var container = document.getElementById(containerId);
container.innerHTML = ";
container.style.gridTemplateColumns = 'repeat(' + cols + ', 60px)';
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var input = document.createElement('input');
input.type = 'number';
input.id = prefix + '_' + i + '_' + j;
input.value = '0';
input.style.width = '60px';
input.style.padding = '5px';
input.style.textAlign = 'center';
input.style.border = '1px solid #ccc';
container.appendChild(input);
}
}
}
function performMultiplication() {
var rA = parseInt(document.getElementById('rowsA').value);
var cA = parseInt(document.getElementById('colsA').value);
var rB = parseInt(document.getElementById('rowsB').value);
var cB = parseInt(document.getElementById('colsB').value);
var resultDiv = document.getElementById('gridResult');
var resultSection = document.getElementById('result-section');
var matrixA = [];
for (var i = 0; i < rA; i++) {
matrixA[i] = [];
for (var j = 0; j < cA; j++) {
var val = parseFloat(document.getElementById('a_' + i + '_' + j).value);
matrixA[i][j] = isNaN(val) ? 0 : val;
}
}
var matrixB = [];
for (var i = 0; i < rB; i++) {
matrixB[i] = [];
for (var j = 0; j < cB; j++) {
var val = parseFloat(document.getElementById('b_' + i + '_' + j).value);
matrixB[i][j] = isNaN(val) ? 0 : val;
}
}
var result = [];
for (var i = 0; i < rA; i++) {
result[i] = [];
for (var j = 0; j < cB; j++) {
var sum = 0;
for (var k = 0; k < cA; k++) {
sum += matrixA[i][k] * matrixB[k][j];
}
result[i][j] = sum;
}
}
resultDiv.innerHTML = '';
resultDiv.style.gridTemplateColumns = 'repeat(' + cB + ', auto)';
for (var i = 0; i < rA; i++) {
for (var j = 0; j < cB; j++) {
var cell = document.createElement('div');
cell.style.padding = '10px';
cell.style.border = '1px solid #27ae60';
cell.style.background = '#ebf5fb';
cell.style.textAlign = 'center';
cell.style.minWidth = '60px';
cell.style.fontWeight = 'bold';
cell.innerText = Number.isInteger(result[i][j]) ? result[i][j] : result[i][j].toFixed(2);
resultDiv.appendChild(cell);
}
}
resultSection.style.display = 'block';
resultSection.scrollIntoView({ behavior: 'smooth' });
}