mtimes,*
Matrix multiplication
Syntax
Description
矩阵乘积的吗C
=A
*B
A
andB
. IfA
is an m-by-p andB
is a p-by-n matrix, thenC
is an m-by-n matrix defined by
This definition says thatC(i,j)
is the inner product of thei
th row ofA
with thej
th column ofB
. You can write this definition using the MATLAB®colon operator as
C(i,j) = A(i,:)*B(:,j)
A
andB
, the number of columns ofA
must equal the number of rows ofB
. Matrix multiplication isnotuniversally commutative for nonscalar inputs. That is,A*B
is typically not equal toB*A
. If at least one input is scalar, thenA*B
是equivalent toA.*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
, whereA
is 500-by-2,B
is 2-by-500, andC
is 500-by-2.With no parentheses, theorder of operationsis left to right so
A*B
is calculated first, which forms a 500-by-500 matrix. This matrix is then multiplied withC
to arrive at the 500-by-2 result.If you instead specify
A*(B*C)
, thenB*C
is multiplied first, producing a 2-by-2 matrix. The small matrix then multipliesA
to arrive at the same 500-by-2 result, but with fewer operations and less intermediate memory usage.