mtimes, *
Matrix multiplication
Syntax
Description
is the matrix product of C = A*BA 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
This definition says that C(i,j) is the inner product of
the ith row of A with the
jth 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 not universally commutative for
nonscalar inputs. That 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.Examples
Input Arguments
Output Arguments
Tips
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 withA*B*C, whereAis 500-by-2,Bis 2-by-500, andCis 500-by-2.With no parentheses, the order of operations is left to right so
A*Bis calculated first, which forms a 500-by-500 matrix. This matrix is then multiplied withCto arrive at the 500-by-2 result.If you instead specify
A*(B*C), thenB*Cis multiplied first, producing a 2-by-2 matrix. The small matrix then multipliesAto arrive at the same 500-by-2 result, but with fewer operations and less intermediate memory usage.
References
[1] “BLAS (Basic Linear Algebra Subprograms).” Accessed July 18, 2022. https://netlib.org/blas/.
[2] Davis, Timothy A. “Algorithm 1000: SuiteSparse:GraphBLAS: Graph Algorithms in the Language of Sparse Linear Algebra.” ACM Transactions on Mathematical Software 45, no. 4 (December 31, 2019): 1–25. https://doi.org/10.1145/3322125.