A matrix is a rectangular array of numbers arranged in rows and columns. In linear algebra, matrices are fundamental tools used to solve systems of linear equations, transform geometric objects, and handle data in computer science.
Matrix Addition and Subtraction
To add or subtract two matrices, they must have the same dimensions. You simply perform the operation on the corresponding elements. For example, in a 3×3 matrix, the element at row 1, column 1 of Matrix A is added to the element at row 1, column 1 of Matrix B.
Matrix multiplication is more complex. The resulting element at row i and column j is calculated by taking the dot product of the i-th row of the first matrix and the j-th column of the second matrix.
Key Rules for Matrix Math:
Commutative Property: Addition is commutative (A + B = B + A), but multiplication is NOT (A × B != B × A).
Identity Matrix: A square matrix with 1s on the diagonal and 0s elsewhere acts like the number "1" in normal multiplication.
Zero Matrix: A matrix where all elements are zero acts like the number "0".
How to use this calculator
Simply enter your values into the 3×3 grids for Matrix A and Matrix B. If you are working with 2×2 matrices, leave the third row and third column as zeros. Select the desired operation (Addition, Subtraction, or Multiplication) to see the instantaneous result.
function getCellValue(id) {
var val = document.getElementById(id).value;
return parseFloat(val) || 0;
}
function calculateMatrix(operation) {
var a = [
[getCellValue('a00'), getCellValue('a01'), getCellValue('a02')],
[getCellValue('a10'), getCellValue('a11'), getCellValue('a12')],
[getCellValue('a20'), getCellValue('a21'), getCellValue('a22')]
];
var b = [
[getCellValue('b00'), getCellValue('b01'), getCellValue('b02')],
[getCellValue('b10'), getCellValue('b11'), getCellValue('b12')],
[getCellValue('b20'), getCellValue('b21'), getCellValue('b22')]
];
var result = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
var label = "";
if (operation === 'add') {
label = "Result of Matrix Addition (A + B)";
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
} else if (operation === 'sub') {
label = "Result of Matrix Subtraction (A – B)";
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
result[i][j] = a[i][j] – b[i][j];
}
}
} else if (operation === 'mul') {
label = "Result of Matrix Multiplication (A × B)";
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var sum = 0;
for (var k = 0; k < 3; k++) {
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
}
displayResult(result, label);
}
function displayResult(res, labelText) {
var container = document.getElementById('matrix-result-container');
var grid = document.getElementById('resultGrid');
var label = document.getElementById('result-label');
label.innerText = labelText;
grid.innerHTML = '';
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
var cell = document.createElement('div');
cell.className = 'result-cell';
// Round to 4 decimal places to avoid floating point issues
cell.innerText = Math.round(res[i][j] * 10000) / 10000;
grid.appendChild(cell);
}
}
container.style.display = 'block';
container.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
function resetMatrices() {
var inputs = document.querySelectorAll('.matrix-grid input');
for (var i = 0; i < inputs.length; i++) {
inputs[i].value = 0;
}
document.getElementById('matrix-result-container').style.display = 'none';
}