Main Content

Matrix multiplication

is the matrix product of `C`

= `A`

*`B`

`A`

and `B`

. If
`A`

is an m-by-p and `B`

is a p-by-n
matrix, then `C`

is an m-by-n matrix defined by

$$C(i,j)={\displaystyle \sum _{k=1}^{p}A}(i,k)B(k,j).$$

This definition says that `C(i,j)`

is the inner product of
the `i`

th row of `A`

with the
`j`

th column of `B`

. You can write this
definition using the MATLAB^{®} colon operator as

C(i,j) = A(i,:)*B(:,j)

`A`

and `B`

, the number of columns
of `A`

must equal the number of rows of `B`

.
Matrix multiplication is `A*B`

is typically not equal to
`B*A`

. If at least one input is scalar, then
`A*B`

is equivalent to `A.*B`

and is
commutative.With chained matrix multiplications such as

`A*B*C`

, you might be able to improve execution time by using parentheses to dictate the order of the operations. Consider the case of multiplying three matrices with`A*B*C`

, where`A`

is 500-by-2,`B`

is 2-by-500, and`C`

is 500-by-2.With no parentheses, the order of operations is left to right so

`A*B`

is calculated first, which forms a 500-by-500 matrix. This matrix is then multiplied with`C`

to arrive at the 500-by-2 result.If you instead specify

`A*(B*C)`

, then`B*C`

is multiplied first, producing a 2-by-2 matrix. The small matrix then multiplies`A`

to arrive at the same 500-by-2 result, but with fewer operations and less intermediate memory usage.

`colon`

| `cross`

| `dot`

| `pagemtimes`

| `times`