Matrix Multiplication Calculator

Pick the shape of each matrix (anything from 1x1 up to 4x4, square or not), fill in the entries, and get A times B with every cell of the answer worked out term by term. When B times A is also possible, you get both orders side by side, which is the quickest way there is to see why order matters.

Put this calculator on your website for free

Copy one snippet and give your visitors a working Matrix Multiplication Calculator.

How multiplying matrices works

Every cell of the answer is built the same way: take a row of A, take a column of B, lay them side by side, multiply the pairs, and add the products up. That one move, row times column, is the whole operation. It also explains the famous entry requirement: a row of A holds as many numbers as A has columns, a column of B holds as many numbers as B has rows, and the pairing only works when those two counts agree. So an m×n matrix times an n×p matrix works exactly when the inner numbers match, and the outer numbers, m and p, are the shape of the answer. A 2×3 times a 3×2 gives a 2×2. The inner numbers do the work and then vanish.

This page does one operation as deeply as we know how: every cell of the product is written out with your own numbers, and whenever the reverse order B × A is also legal, you get both products side by side. Addition, subtraction, determinants and inverses live on the matrix calculator; multiplication earned its own page.

The formula

(A×B)ij = Σk aik·bkj     (m×n)·(n×p) → m×p

Here aik is the entry in row i, column k of A, and bkj is the entry in row k, column j of B. The sum over k is the row-dot-column move described above: k walks along row i of A and down column j of B at the same time, and the products pile up into one number, which becomes row i, column j of the answer.

Worked example

Multiply a 2×3 by a 3×2. Let A = [[1, 2, 3],[4, 5, 6]] and B = [[7, 8],[9, 10],[11, 12]]. The inner numbers are 3 and 3, so the product exists, and the outer numbers say the answer is 2×2. Top left: 1×7 + 2×9 + 3×11 = 7 + 18 + 33 = 58. Top right: 1×8 + 2×10 + 3×12 = 64. Bottom left: 4×7 + 5×9 + 6×11 = 139. Bottom right: 4×8 + 5×10 + 6×12 = 154. So A × B = [[58, 64],[139, 154]].

Now feed the same two matrices in the other order. B × A is a 3×2 times a 2×3, which is also legal, and it comes out 3×3: nine entries instead of four. Same ingredients, different order, and not even the same shape of answer. Run it above and watch.

Why the rule is row times column at all

The rule looks arbitrary until you see what it is for. A row-times-column sum is a weighted combination: one list of weights meeting one list of values, each value scaled by its weight, all of it added into a single number. That is the most useful move in applied math. An invoice total is a row of quantities times a column of prices. A course grade is a row of category weights times a column of your scores. Matrix multiplication is nothing more than doing that weighted combination for every row of A against every column of B at once, and organizing the answers into a grid. When the result is a single number, one row against one column, it has its own name, the dot product, and its own page: the dot product calculator.

The reason your graphics card is an AI chip

A neural network layer is a matrix multiplication. Not something like one: it is one. The layer's learned weights sit in a matrix, the inputs arrive as a column, and each output neuron is one row-times-column sum, a weighted combination of every input. Here is the whole idea at house scale. Put three houses in a 3×3 matrix, one row per house, with columns for size in hundreds of square feet, bedrooms, and age in decades: [[14, 3, 2],[20, 4, 1],[11, 2, 3]]. Now score them with a 3×1 column of weights, 5 points per hundred square feet, 10 per bedroom, minus 4 per decade of age: [[5],[10],[-4]]. The product is a 3×1 column of scores of 92, 136 and 63, one weighted judgment per house, all three computed in a single multiplication. Feed those numbers through the calculator above and you can watch each score get built term by term.

A real model does exactly this with matrices thousands of rows wide, chained layer after layer, billions of multiply-and-add steps per answer. Graphics cards happen to be machines built to do enormous numbers of these products every second, because moving and lighting 3D scenes is matrix multiplication too. That is the entire story of why the chip that ran your games became the chip that runs AI: same operation, new customer.

AB and BA: order is part of the question

With numbers, 3 × 5 and 5 × 3 are the same thing, so it is natural to assume matrices work the same way. They do not, and this page would rather show you than tell you: whenever both orders are legal, it computes both and puts them side by side. Three things can happen, and all three are instructive. The two products can differ cell by cell, which is the normal case. They can come out different shapes entirely, a 2×2 in one order and a 3×3 in the other. Or one order can be flatly impossible while the other works fine, and the shapes alone settle it before any arithmetic starts. Once you have watched a pair of matrices give two different answers, non-commutativity stops being a rule to memorize and becomes something you saw happen. The rare pairs that do commute, two diagonal matrices, anything with the identity, a matrix with its own powers, get noticed and congratulated when they turn up.

The identity, and the rule that survives

Matrix multiplication does keep a version of the number 1: the identity matrix I, with ones down the diagonal and zeros everywhere else. Multiply any matrix by I, on either side, and it comes back untouched. Enter one above and watch it happen; the page will point it out.

And one law does survive the loss of commutativity: associativity. (AB)C = A(BC), always. You may not reorder the factors, but you may group them however you like, and that freedom is quietly load-bearing. It is why a chain of neural network layers makes sense at all: the network can be read as one long matrix product, and associativity guarantees that applying the layers one at a time gives the same answer as collapsing any stretch of them into a single matrix first. Order of factors is sacred; order of grouping is free.

Frequently asked questions

Why do the inner dimensions have to match?

Because every cell of the answer is a row of A paired with a column of B: multiply the pairs, add them up. A row of A holds as many numbers as A has columns, and a column of B holds as many numbers as B has rows. If those two counts differ, the pairing breaks down with numbers left over, so the product is simply not defined. There is nothing to round or approximate; the shapes either fit or they do not.

Is A times B the same as B times A?

Almost never, and this page shows you rather than tells you: whenever both orders are legal it computes both and puts them side by side. Sometimes the two answers are different matrices, sometimes they are not even the same shape, and sometimes B times A does not exist at all while A times B does. The pairs that do commute are special: two diagonal matrices, any matrix with the identity, a matrix with its own powers.

What shape is the result of multiplying an m x n matrix by an n x p matrix?

m x p: the outer numbers. A 2x3 times a 3x4 gives a 2x4. The inner numbers (3 and 3 here) have to match for the product to exist, and then they disappear from the answer entirely. The result has as many rows as A and as many columns as B.

Can you multiply non-square matrices?

Yes, and most real work does. A spreadsheet of 100 houses with 3 features each is a 100x3 matrix, and scoring every house with one 3x1 column of weights is a 100x3 times 3x1 multiplication. The only requirement is that A's columns match B's rows. Square matrices are the textbook case, not the common one.

What happens when you multiply by the identity matrix?

Nothing, and that is the point. The identity matrix I (ones on the diagonal, zeros everywhere else) is the 1 of matrix multiplication: A times I and I times A both give back A untouched. This calculator notices when one of your matrices is an identity and says so, because watching a matrix pass through unchanged is the best way to believe it.

How do you multiply two matrices by hand?

One cell at a time. To fill row i, column j of the answer, run your left hand along row i of A and your right hand down column j of B, multiply each pair you touch, and add the products up. For [[1,2],[3,4]] times [[5,6],[7,8]], the top left cell is 1×5 + 2×7 = 19. Repeat for every cell. This page prints exactly that arithmetic for each cell of your own matrices.

How does matrix multiplication relate to neural networks?

A neural network layer is a matrix multiplication: a matrix of learned weights times a column of inputs, giving each output as a weighted combination of every input. Modern AI models chain thousands of these products over enormous matrices, which is why graphics cards, chips built to do exactly this operation very fast for 3D games, became the hardware of AI.

Related calculators