kron
Kronecker tensor product
Syntax
Description
K = kron(
returns theKronecker tensor product矩阵的A,B
)A
和B
. IfA
is anm
-by-n
matrix andB
is ap
-by-q
然后kron(A,B)
is anm*p
-by-n*q
matrix formed by taking all possible products between the elements ofA
和the matrixB
.
Examples
Block Diagonal Matrix
Create a block diagonal matrix.
创建一个4 x-4的身份矩阵和一个2 by-2矩阵,您希望沿对角线重复。
一个=眼(4);B = (1 -1;-1 1];
Usekron
to find the Kronecker tensor product.
K = kron(A,B)
K =8×81 -1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 -1 1
The result is an 8-by-8 block diagonal matrix.
Repeat Matrix Elements
Expand the size of a matrix by repeating elements.
Create a 2-by-2 matrix of ones and a 2-by-3 matrix whose elements you want to repeat.
A = [1 2 3; 4 5 6]; B = ones(2);
Calculate the Kronecker tensor product usingkron
.
K = kron(A,B)
K =4×61 1 2 2 3 3 1 1 2 2 3 3 4 4 5 5 6 6 4 4 5 5 6 6
The result is a 4-by-6 block matrix.
Sparse Laplacian Operator Matrix
This example visualizes a sparse Laplacian operator matrix.
The matrix representation of the discrete Laplacian operator on a two-dimensional,n
-by-n
grid is an*n
-by-n*n
sparse matrix. There are at most five nonzero elements in each row or column. You can generate the matrix as the Kronecker product of one-dimensional difference operators. In this examplen = 5
.
n = 5;i = speye(n,n);e =稀疏(2:n,1:n-1,1,n,n);d = e+e'-2*i;a = kron(d,i)+kron(i,d);
Visualize the sparsity pattern with间谍
.
间谍(A,'k')
Input Arguments
A,B
—Input matrices
标量|vectors|矩阵
Input matrices, specified as scalars, vectors, or matrices. If eitherA
或者B
稀疏,然后kron
multiplies only nonzero elements and the result is also sparse.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
Complex Number Support:Yes
More About
Kronecker Tensor Product
IfA
is anm
-by-n
matrix andB
is ap
-by-q
然后the Kronecker tensor product ofA
和B
is a large matrix formed by multiplyingB
by each element ofA
For example, two simple 2-by-2 matrices produce
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
使用GPU CODER™为NVIDIA®GPU生成CUDA®代码。
Usage notes and limitations:
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
或者accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
使用并行计算工具箱™在图形处理单元(GPU)上运行加速代码。
This function fully supports GPU arrays. For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Version History
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- AméricaLatina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- 丹麦(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- 法国(Français)
- 爱尔兰(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- 挪威(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)