Main Content
Matrix multiplication
collapse all in page
Syntax
C = A*B
C = mtimes(A,B)
Description
C = A*B
is the matrix product of 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
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)
For nonscalar 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.
example
C = mtimes(A,B)
is an alternative way to execute A*B
, but is rarely used. It enables operator overloading for classes.
Examples
collapse all
Multiply Two Vectors
Open Live Script
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];
Multiply A
times B
.
C = A*B
C = 3
The result is a 1-by-1 scalar, also called the dot product or inner product of the vectors A
and B
. Alternatively, you can calculate the dot product with the syntax dot(A,B)
.
Multiply B
times A
.
C = B*A
C = 4×4 1 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 the outer product of the vectors A
and B
. The outer product of two vectors, , returns a matrix.
Multiply Two Arrays
Open Live Script
Create two arrays, A
and B
.
A = [1 3 5; 2 4 7];B = [-5 8 11; 3 9 21; 4 0 8];
Calculate the product of A
and B
.
C = A*B
C = 2×3 24 35 114 30 52 162
Calculate the inner product of the second row of A
and the third column of B
.
A(2,:)*B(:,3)
ans = 162
This answer is the same as C(2,3)
.
Input Arguments
collapse all
A
, B
— Operands
scalars | vectors | matrices
Operands, specified as scalars, vectors, or matrices.
If at least one input is scalar, then
A*B
is equivalent toA.*B
. In this case, the nonscalar array can be any size.For nonscalar inputs,
A
andB
must be 2-D arrays where the number of columns inA
must be equal to the number of rows inB
.If one of
A
orB
is 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
C
— Product
scalar | vector | matrix
Product, returned as a scalar, vector, or matrix. Array C
has the same number of rows as input A
and the same number of columns as input B
. For example, if A
is an m-by-0 empty matrix and B
is a 0-by-n empty matrix, then A*B
is an m-by-n matrix of zeros.
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, 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 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.
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.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitations:
For
A*B
whereA
andB
are both tall arrays, one of them must be a scalar.For
A'*B
, bothA
andB
mustbe tall vectors or matrices with a common size in the first dimension.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Multiplication of pure imaginary numbers by non-finite numbers might not match MATLAB. The code generator does not specialize multiplication by pure imaginary numbers—it does not eliminate calculations with the zero real part. For example,
(Inf + 1i)*1i = (Inf*0 – 1*1) + (Inf*1 + 1*0)i = NaN + Infi
.See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
Multiplication of pure imaginary numbers by non-finite numbers might not match MATLAB. The code generator does not specialize multiplication by pure imaginary numbers—it does not eliminate calculations with the zero real part. For example,
(Inf + 1i)*1i = (Inf*0 – 1*1) + (Inf*1 + 1*0)i = NaN + Infi
.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The mtimes
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
expand all
R2022a: Improved performance when multiplying sparse and full matrices
Matrix multiplication shows improved performance when:
One of the operands is a sparse matrix, and the other is a full matrix.
The sparse operand has at least 50,000 nonzero elements.
The full operand has at least 32 columns (or at least 32 rows when transposed).
The performance improvement arises from added support for multithreading in the operation, and therefore the speedup improves as the matrix size and number of nonzero elements increase.
For example, multiplying a 102,400-by-102,400 sparse matrix with a 102,400-by-128 full matrix on a machine with 6 physical cores is about 2.7x faster than in the previous release.
function timingSparseDenseMultA = delsq(numgrid('S',322));B = rand(size(A,2),128);ticfor k = 1:10 C = A*B;endtocend
The approximate execution times are:
R2021b: 0.8 s
R2022a: 0.3 s
The code was timed on a Windows® 10, Intel® Xeon® CPU W-2133 @ 3.60 GHz test system by calling the timingSparseDenseMult
function.
See Also
colon | times | dot | cross | pagemtimes | tensorprod
Topics
- Array vs. Matrix Operations
- Operator Precedence
- MATLAB Operators and Special Characters
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office