Main Content

mtimes,*

Matrix multiplication

Description

example

C=A*B矩阵乘积的吗AandB. IfAis an m-by-p andBis a p-by-n matrix, thenCis an m-by-n matrix defined by

C ( i , j ) = k = 1 p A ( i , k ) B ( k , j ) .

This definition says thatC(i,j)is the inner product of theith row ofAwith thejth column ofB. You can write this definition using the MATLAB®colon operator as

C(i,j) = A(i,:)*B(:,j)
For nonscalarAandB, the number of columns ofAmust equal the number of rows ofB. Matrix multiplication isnotuniversally commutative for nonscalar inputs. That is,A*Bis typically not equal toB*A. If at least one input is scalar, thenA*B是equivalent toA.*Band is commutative.

C= mtimes(A,B)is an alternative way to executeA*B, but is rarely used. It enables operator overloading for classes.

Examples

collapse all

Create a 1-by-4 row vector,A, and a 4-by-1 column vector,B.

A = [1 1 0 0]; B = [1; 2; 3; 4];

MultiplyAtimesB.

C = A*B
C = 3

The result is a 1-by-1 scalar, also called the点productorinner productof the vectorsAandB. Alternatively, you can calculate the dot product A B with the syntax点(A,B).

MultiplyBtimesA.

C = B*A
C =4×41 1 0 0 2 2 0 0 3 3 0 0 4 4 0 0

The result is a 4-by-4 matrix, also called theouter productof the vectorsAandB. The outer product of two vectors, A B , returns a matrix.

Create two arrays,AandB.

A = [1 3 5; 2 4 7]; B = [-5 8 11; 3 9 21; 4 0 8];

Calculate the product ofAandB.

C = A*B
C =2×324 35 114 30 52 162

Calculate the inner product of the second row ofAand the third column ofB.

A(2,:)*B(:,3)
ans = 162

This answer is the same asC(2,3).

Input Arguments

collapse all

Operands, specified as scalars, vectors, or matrices.

  • If at least one input is scalar, thenA*B是equivalent toA.*B. In this case, the nonscalar array can be any size.

  • For nonscalar inputs,AandBmust be 2-D arrays where the number of columns inAmust be equal to the number of rows inB.

  • If one ofAorBis an integer class (int16,uint8, …), then the other input must be a scalar. Operands with an integer data type cannot be complex.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|duration|calendarDuration
Complex Number Support:Yes

Output Arguments

collapse all

Product, returned as a scalar, vector, or matrix. ArrayChas the same number of rows as inputAand the same number of columns as inputB. For example, ifAis an m-by-0 empty matrix andBis a 0-by-n empty matrix, thenA*Bis an m-by-n matrix of zeros.

Tips

  • With chained matrix multiplications such asA*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, theorder of operationsis left to right soA*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 specifyA*(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.

Extended Capabilities

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Introduced before R2006a