Matrix Calculator

Matrix A Input
row  column
  ×
Matrix B Input
row  column
  ×
'; } if (aRowAmt!=aColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } tempArray = aArray; ctDesc = 'A'+ctNum+' ='; }else{ ctNum = document.calcf.pb.value+""; if (!bValid){ hasError = true; errMsg = '
Please provide valid data in Matrix B.
'; } if (bRowAmt!=bColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } tempArray = bArray; ctDesc = 'B'+ctNum+' ='; } if ((ctNum.length<1)||(!isInteger(ctNum))){ errMsg += '
Please provide a positive integer power value.
'; hasError = true; }else{ ctNum = parseInt(ctNum); if (ctNum<1){ errMsg += '
Please provide a positive integer power value.
'; hasError = true; } } if (hasError){ showErr("resultout"+mname, errMsg); }else{ resultArray = tempArray; var tempSize = tempArray.length; for (var m = 0; m < (ctNum-1); m++) { var newResultA = new Array(); for (var i = 0; i < tempSize; i++) { newResultA[i] = new Array(); for (var j = 0; j < tempSize; j++){ newResultA[i][j] = 0; for (var k = 0; k < tempSize; k++){ newResultA[i][j] += resultArray[i][k]*tempArray[k][j]; } } } resultArray = newResultA; } showResult("resultout"+mname, ctDesc, showArray(resultArray)); } return false; } function cTimes(mname){ cClear(); var ctNum = 1; var tempArray = new Array(); var ctDesc = ''; if (mname=='a'){ ctNum = document.calcf.ta.value+""; if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } tempArray = aArray; ctDesc = 'A × '+ctNum+' ='; }else{ ctNum = document.calcf.tb.value+""; if (!bValid){ hasError = true; errMsg = '
Please provide valid data in Matrix B.
'; } tempArray = bArray; ctDesc = 'B × '+ctNum+' ='; } if ((ctNum.length<1)||(!isNumber(ctNum))){ errMsg += '
Please provide a value to multiple.
'; hasError = true; } if (hasError){ showErr("resultout"+mname, errMsg); }else{ ctNum = parseFloat(ctNum); for (var i = 0; i < tempArray.length; i++) { resultArray[i] = new Array(); for (var j = 0; j < tempArray[i].length; j++){ resultArray[i][j] = tempArray[i][j]*ctNum; } } showResult("resultout"+mname, ctDesc, showArray(resultArray)); } return false; } function findDet(fdArray, fdSize){ if (fdSize<3){ if (fdSize<2){ return fdArray[0][0]; }else{ return (fdArray[0][0]*fdArray[1][1] - fdArray[0][1]*fdArray[1][0]); } }else{ var signVal = -1; var tempOutVal = 0; for (var i = 0; i < fdSize; i++) { signVal *= -1; var tempArray = new Array(); var c = 0; for (var j = 0; j < fdSize; j++) { if (i!=j){ tempArray[c] = new Array(); for (var k = 1; k < fdSize; k++) { tempArray[c][k-1] = fdArray[j][k]; } c++; } } tempOutVal += signVal*fdArray[i][0]*findDet(tempArray, (fdSize-1)); } return tempOutVal; } } function cDeterminant(mname){ cClear(); var cdSize = 1; var tempArray = new Array(); var ctDesc = ''; if (mname=='a'){ if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } if (aRowAmt!=aColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } cdSize = aRowAmt; tempArray = aArray; ctDesc = 'Determinant of A ='; }else{ if (!bValid){ hasError = true; errMsg = '
Please provide valid data in Matrix B.
'; } if (bRowAmt!=bColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } cdSize = bRowAmt; tempArray = bArray; ctDesc = 'Determinant of B ='; } if (hasError){ showErr("resultout"+mname, errMsg); }else{ document.getElementById("resultout"+mname).innerHTML = '

Result

'+ctDesc+' ' + findDet(tempArray, cdSize) + '

'; //showResult("resultout"+mname, ctDesc, findDet(tempArray, cdSize)); } return false; } function cInverse(mname){ cClear(); var cdSize = 1; var tempArray = new Array(); var ctDesc = ''; if (mname=='a'){ if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } if (aRowAmt!=aColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } cdSize = aRowAmt; tempArray = aArray; ctDesc = 'A-1 ='; }else{ if (!bValid){ hasError = true; errMsg = '
Please provide valid data in Matrix B.
'; } if (bRowAmt!=bColumnAmt){ hasError = true; errMsg += '
The row and column size need to be the same.
'; } cdSize = bRowAmt; tempArray = bArray; ctDesc = 'B-1 ='; } if (hasError){ showErr("resultout"+mname, errMsg); }else{ var tempDetVal = findDet(tempArray, cdSize); if (tempDetVal==0){ showErr("resultout"+mname, '
Determinant is 0. Inverse does not exist.
'); }else{ //create Cofactor Matrix cdSize if (cdSize<2){ resultArray[0] = new Array(); resultArray[0][0] = 1/tempArray[0][0]; }else{ for (var i = 0; i < cdSize; i++) { resultArray[i] = new Array(); for (var j = 0; j < cdSize; j++){ var signVal = Math.pow(-1, (i+j)); var x = 0; var y = 0; var tempArray2 = new Array(); for (var m = 0; m < cdSize; m++) { if (m!=i){ tempArray2[x] = new Array(); y = 0; for (var n = 0; n < cdSize; n++) { if (n!=j){ tempArray2[x][y] = tempArray[m][n]; y++; } } x++; } } resultArray[i][j] = signVal*findDet(tempArray2, (cdSize-1)); } } for (var i = 0; i < cdSize; i++) { for (var j = 0; j < cdSize; j++){ tempArray[j][i] = resultArray[i][j]/tempDetVal; } } resultArray = tempArray; } showResult("resultout"+mname, ctDesc, showArray(resultArray)); } } return false; } function cTranspose(mname){ cClear(); var ctNum = 1; var tempArray = new Array(); var ctDesc = ''; if (mname=='a'){ if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } tempArray = aArray; ctDesc = 'AT ='; }else{ if (!bValid){ hasError = true; errMsg = '
Please provide valid data in Matrix B.
'; } tempArray = bArray; ctDesc = 'BT ='; } if (hasError){ showErr("resultout"+mname, errMsg); }else{ for (var i = 0; i < tempArray[0].length; i++) { resultArray[i] = new Array(); for (var j = 0; j < tempArray.length; j++){ resultArray[i][j] = tempArray[j][i]; } } showResult("resultout"+mname, ctDesc, showArray(resultArray)); } return false; } function cAdd(){ cClear(); if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } if (!bValid){ hasError = true; errMsg += '
Please provide valid data in Matrix B.
'; } if (hasError){ showErr("resultout", errMsg); }else{ if ((aRowAmt==bRowAmt)&&(aColumnAmt==bColumnAmt)){ for (var i = 0; i < aRowAmt; i++) { resultArray[i] = new Array(); for (var j = 0; j < aColumnAmt; j++){ resultArray[i][j] = aArray[i][j] + bArray[i][j]; } } showResult("resultout", 'A + B =', showArray(resultArray)); }else{ showErr("resultout", '
Matrix A and B need to be the same in size.
'); } } return false; } function cSub(){ cClear(); if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } if (!bValid){ hasError = true; errMsg += '
Please provide valid data in Matrix B.
'; } if (hasError){ showErr("resultout", errMsg); }else{ if ((aRowAmt==bRowAmt)&&(aColumnAmt==bColumnAmt)){ for (var i = 0; i < aRowAmt; i++) { resultArray[i] = new Array(); for (var j = 0; j < aColumnAmt; j++){ resultArray[i][j] = aArray[i][j] - bArray[i][j]; } } showResult("resultout", 'A – B =', showArray(resultArray)); }else{ showErr("resultout", '
Matrix A and B need to be the same in size.
'); } } return false; } function cMultiple(){ cClear(); if (!aValid){ hasError = true; errMsg = '
Please provide valid data in Matrix A.
'; } if (!bValid){ hasError = true; errMsg += '
Please provide valid data in Matrix B.
'; } if (hasError){ showErr("resultout", errMsg); }else{ if (aColumnAmt==bRowAmt){ for (var i = 0; i < aRowAmt; i++) { resultArray[i] = new Array(); for (var j = 0; j < bColumnAmt; j++){ resultArray[i][j] = 0; for (var k = 0; k < aColumnAmt; k++){ resultArray[i][j] += aArray[i][k]*bArray[k][j]; } } } showResult("resultout", 'AB =', showArray(resultArray)); }else{ showErr("resultout", '
Matrix A column and B row amount need to be the same.
'); } } return false; } function cSwap(){ cClear(); readInput('a'); readInput('b'); var tempRow = document.calcf.arow.value; var tempColumn = document.calcf.acolumn.value; document.calcf.arow.value = document.calcf.brow.value; document.calcf.acolumn.value = document.calcf.bcolumn.value; document.calcf.brow.value = tempRow; document.calcf.bcolumn.value = tempColumn; var tempArray = matrixa; matrixa = matrixb; matrixb = tempArray; updateInput('a'); updateInput('b'); document.getElementById("resultout").innerHTML = '

Result

Matrix A and B were swapped.
'; return false; } function cpToA(){ var ctRow = resultArray.length; var ctColumn = resultArray[0].length; document.calcf.arow.value = ctRow; document.calcf.acolumn.value = ctColumn; for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { matrixa[i][j] = ''; } } for (var i = 0; i < ctRow; i++) { for (var j = 0; j < ctColumn; j++){ matrixa[i][j] = resultArray[i][j]; } } updateInput('a'); return false; } function cpToB(){ var ctRow = resultArray.length; var ctColumn = resultArray[0].length; document.calcf.brow.value = ctRow; document.calcf.bcolumn.value = ctColumn; for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { matrixb[i][j] = ''; } } for (var i = 0; i < ctRow; i++) { for (var j = 0; j < ctColumn; j++){ matrixb[i][j] = resultArray[i][j]; } } updateInput('b'); return false; }

A matrix, in a mathematical context, is a rectangular array of numbers, symbols, or expressions that are arranged in rows and columns. Matrices are often used in scientific fields such as physics, computer graphics, probability theory, statistics, calculus, numerical analysis, and more.

The dimensions of a matrix, A, are typically denoted as m × n. This means that A has m rows and n columns. When referring to a specific value in a matrix, called an element, a variable with two subscripts is often used to denote each element based on its position in the matrix. For example, given ai,j, where i = 1 and j = 3, a1,3 is the value of the element in the first row and the third column of the given matrix.

Matrix operations such as addition, multiplication, subtraction, etc., are similar to what most people are likely accustomed to seeing in basic arithmetic and algebra, but do differ in some ways, and are subject to certain constraints. Below are descriptions of the matrix operations that this calculator can perform.

Matrix addition

Matrix addition can only be performed on matrices of the same size. This means that you can only add matrices if both matrices are m × n. For example, you can add two or more 3 × 3, 1 × 2, or 5 × 4 matrices. You cannot add a 2 × 3 and a 3 × 2 matrix, a 4 × 4 and a 3 × 3, etc. The number of rows and columns of all the matrices being added must exactly match.

If the matrices are the same size, matrix addition is performed by adding the corresponding elements in the matrices. For example, given two matrices, A and B, with elements ai,j, and bi,j, the matrices are added by adding each element, then placing the result in a new matrix, C, in the corresponding position in the matrix:

A =
12
34
; B =
56
78

In the above matrices, a1,1 = 1; a1,2 = 2; b1,1 = 5; b1,2 = 6; etc. We add the corresponding elements to obtain ci,j. Adding the values in the corresponding rows and columns:

a1,1 + b1,1 = 1 + 5 = 6 = c1,1
a1,2 + b1,2 = 2 + 6 = 8 = c1,2
a2,1 + b2,1 = 3 + 7 = 10 = c2,1
a2,2 + b2,2 = 4 + 8 = 12 = c2,2

Thus, matrix C is:

C =
68
1012

Matrix subtraction

Matrix subtraction is performed in much the same way as matrix addition, described above, with the exception that the values are subtracted rather than added. If necessary, refer to the information and examples above for a description of notation used in the example below. Like matrix addition, the matrices being subtracted must be the same size. If the matrices are the same size, then matrix subtraction is performed by subtracting the elements in the corresponding rows and columns:

A =
12
34
; B =
56
78
a1,1 - b1,1 = 1 - 5 = -4 = c1,1
a1,2 - b1,2 = 2 - 6 = -4 = c1,2
a2,1 - b2,1 = 3 - 7 = -4 = c2,1
a2,2 - b2,2 = 4 - 8 = -4 = c2,2

Thus, matrix C is:

C =
-4-4
-4-4

Matrix multiplication

Scalar multiplication:

Matrices can be multiplied by a scalar value by multiplying each element in the matrix by the scalar. For example, given a matrix A and a scalar c:

A =
12
34
; c = 5

The product of c and A is:

5 ×
12
34
=
510
1520

Matrix-matrix multiplication:

Multiplying two (or more) matrices is more involved than multiplying by a scalar. In order to multiply two matrices, the number of columns in the first matrix must match the number of rows in the second matrix. For example, you can multiply a 2 × 3 matrix by a 3 × 4 matrix, but not a 2 × 3 matrix by a 4 × 3.

Can be multiplied:

A =
a1,1a1,2a1,3
a2,1a2,2a2,3
; B =
b1,1b1,2b1,3b1,4
b2,1b2,2b2,3b2,4
b3,1b3,2b3,3b3,4

Cannot be multiplied:

A =
a1,1a1,2a1,3
a2,1a2,2a2,3
; B =
b1,1b1,2b1,3
b2,1b2,2b2,3
b3,1b3,2b3,3
b4,1b4,2b4,3

Note that when multiplying matrices, A × B does not necessarily equal B × A. In fact, just because A can be multiplied by B doesn't mean that B can be multiplied by A.

If the matrices are the correct sizes, and can be multiplied, matrices are multiplied by performing what is known as the dot product. The dot product involves multiplying the corresponding elements in the row of the first matrix, by that of the columns of the second matrix, and summing up the result, resulting in a single value. The dot product can only be performed on sequences of equal lengths. This is why the number of columns in the first matrix must match the number of rows of the second.

The dot product then becomes the value in the corresponding row and column of the new matrix, C. For example, from the section above of matrices that can be multiplied, the blue row in A is multiplied by the blue column in B to determine the value in the first column of the first row of matrix C. This is referred to as the dot product of row 1 of A and column 1 of B:

a1,1×b1,1 + a1,2×b2,1 + a1,3×b3,1 = c1,1

The dot product is performed for each row of A and each column of B until all combinations of the two are complete in order to find the value of the corresponding elements in matrix C. For example, when you perform the dot product of row 1 of A and column 1 of B, the result will be c1,1 of matrix C. The dot product of row 1 of A and column 2 of B will be c1,2 of matrix C, and so on, as shown in the example below:

A =
121
341
; B =
5611
7811
1111

When two matrices are multiplied, the product inherits the row count of the first operand (A) and the column count of the second (B). In our example, A measures 2 × 3 while B is 3 × 4. Consequently, the product C will be a 2 × 4 matrix. The colour coding helps you quickly see whether the multiplication is legal and what the size of the resulting matrix will be. Below we compute each entry of C by taking dot‑products of rows of A with columns of B:

C =
202344
445188

Below, the calculation of the dot product for each row and column of C is shown:

c1,1 = 1×5 + 2×7 + 1×1 = 20
c1,2 = 1×6 + 2×8 + 1×1 = 23
c1,3 = 1×1 + 2×1 + 1×1 = 4
c1,4 = 1×1 + 2×1 + 1×1 = 4
c2,1 = 3×5 + 4×7 + 1×1 = 44
c2,2 = 3×6 + 4×8 + 1×1 = 51
c2,3 = 3×1 + 4×1 + 1×1 = 8
c2,4 = 3×1 + 4×1 + 1×1 = 8

Power of a matrix

In the context of this tool, “matrix exponentiation’’ means raising a matrix to an integer power. For instance, selecting “Power 2’’ for a matrix A yields A2. The exponent rules are the same as for ordinary numbers, but you must respect the rules of matrix multiplication, so only square matrices (same number of rows and columns) can be exponentiated. A non‑square matrix A cannot be multiplied by itself, making A × A undefined. If you need a refresher on how matrix multiplication works, see the relevant section above. Example:

A =
13
21

A raised to the power of 2 is:

A2 =
13
21
2
=
13
21
×
13
21
=
76
47

As with exponents in other mathematical contexts, A3, would equal A × A × A, A4 would equal A × A × A × A, and so on.

Transpose of a matrix

The transpose operation, usually denoted by a superscript “T”, mirrors a matrix across its main diagonal. This swap turns the entry a_{ij} of matrix A into a_{ji} in the transposed matrix A^{T}. Refer to earlier sections for a recap of the notation.

An m × n matrix, transposed, would therefore become an n × m matrix, as shown in the examples below:

A =
13
21
AT =
12
31
B =
202344
445188
BT =
2044
2351
48
48

Determinant of a matrix

A matrix’s determinant is a scalar derived from the entries of a square matrix. It plays a key role in linear algebra, calculus and related fields—for example, it helps compute a matrix’s inverse or solve systems of linear equations.

Several techniques exist for evaluating a determinant. Among the most common are the Leibniz expansion and the Laplace (cofactor) expansion.

Determinant of a 2 × 2 matrix:

For a 2 × 2 matrix, the determinant can be found with the Leibniz rule, which boils down to a simple arithmetic expression. Given a matrix A:

A =
ab
cd

The determinant of A using the Leibniz formula is:

|A| =
ab
cd
= ad - bc

When writing the determinant, the matrix is normally enclosed in vertical bars “| |”. For instance:

A =
24
68
|A| =
24
68
= 2×8 - 4×6= -8

Determinant of a 3 × 3 matrix:

One approach to obtaining the determinant of a 3 × 3 matrix is the Laplace (cofactor) expansion. Both the Laplace and Leibniz formulas can be expressed mathematically, although they involve notation beyond the scope of this guide. Below is an illustration of the Laplace method applied to a 3 × 3 matrix:

|A| =
abc
def
ghi
=
a
ef
hi
- b
df
gi
+ c
de
gh

From this point, we can use the Leibniz formula for a 2 × 2 matrix to calculate the determinant of the 2 × 2 matrices, and since scalar multiplication of a matrix just involves multiplying all values of the matrix by the scalar, we can multiply the determinant of the 2 × 2 by the scalar as follows:

|A| =
abc
def
ghi
= a(ei-fh) - b(di-fg) + c(dh-eg)

This can further be simplified to:

|A| = aei + bfg + cdh - ceg - bdi - afh

This is the Leibniz formula for a 3 × 3 matrix.

Determinant of a 4 × 4 matrix and higher:

Determinants of 4 × 4 matrices and larger can be tackled similarly to the 3 × 3 case, using either the Laplace or Leibniz expansion. As demonstrated for the 3 × 3 example, a pattern emerges that lets you break down a higher‑order matrix into a sum of products: each term consists of a scalar from the first row multiplied by the determinant of a smaller sub‑matrix. These scalar‑times‑subdeterminant pairs alternate in sign, effectively adding and subtracting as you proceed.

The expansion proceeds by iterating over every element in the matrix’s first row. Each element becomes a coefficient that multiplies the determinant of a reduced matrix obtained by deleting the element’s row and column. This recursive process continues until you reach a base case (typically a 2 × 2 determinant). See the example below for a step‑by‑step illustration.

Here, we first choose element a. The elements in blue are the scalar, a, and the elements that will be part of the 3 × 3 matrix we need to find the determinant of:

|A| =
abcd
efgh
ijkl
mnop
=
a
fgh
jkl
nop
- ...

Next, we choose element b:

abcd
efgh
ijkl
mnop
b
egh
ikl
mop

Continuing in the same manner for elements c and d, and alternating the sign (+ - + - ...) of each term:

|A| =
abcd
efgh
ijkl
mnop
= a
fgh
jkl
nop
- b
egh
ikl
mop
+ c
efh
ijl
mnp
- d
efg
ijk
mno

Proceeding as with the earlier 3 × 3 example, we keep expanding until the 4 × 4 matrix collapses into a single scalar times a 2 × 2 matrix; the determinant of that smaller matrix is then found via Leibniz’s rule. Although this approach becomes labor‑intensive fast, it works for any n × n matrix once the recursive pattern is clear. More streamlined determinant techniques exist, but they rely on additional algebraic ideas and notation.

Inverse of a matrix

The inverse of a matrix A is denoted as A-1, where A-1 is the inverse of A if the following is true:

A×A-1 = A-1×A = I, where I is the identity matrix

Identity matrix:

An identity matrix is a square array whose diagonal entries are all 1 while every off‑diagonal entry is 0. It plays the same role for matrices as the number 1 does for ordinary multiplication: 1·n = n, and similarly A·I = A for any conformable matrix A. Identity matrices exist in every square size; the three examples shown illustrate 2 × 2, 3 × 3, and 4 × 4 cases.

10
01
;  
100
010
001
;  
1000
0100
0010
0001
...

The n × n identity matrix is thus:

In =
100...0
010...0
001...0
...............
000...1

Inverse of a 2 × 2 matrix:

To invert a 2 × 2 matrix, the following equation can be used:

A-1 =
ab
cd
-1
=
1 
d-b
-ca
det(A)
=
1 
d-b
-ca
ad - bc

For example, given:

A =
24
37
A-1 =
1 
7-4
-32
2×7 - 4×3
=
1 
7-4
-32
2
=
3.5-2
-1.51

If you were to test that this is, in fact, the inverse of A you would find that both:

24
37
×
3.5-2
-1.51
and
3.5-2
-1.51
×
24
37

are equal to the identity matrix:

I =
10
01

Inverse of a 3 × 3 matrix:

Finding the inverse of a 3 × 3 matrix involves a rather involved calculation. The formula needed for this operation is displayed below, though we will not carry out the full evaluation here. Consider:

M =
abc
def
ghi
M-1 =
1
det(M)
ABC
DEF
GHI
T
=
1
det(M)
ADG
BEH
CFI

where:

A=ei-fh; B=-(di-fg); C=dh-eg D=-(bi-ch); E=ai-cg; F=-(ah-bg) G=bf-ce; H=-(af-cd); I=ae-bd

4 × 4 and larger get increasingly more complicated, and there are other methods for computing them.

Financial Fitness & Health Math Other