Main Content

kron

Kronecker tensor product

Syntax

Description

example

K = kron(A,B)returns theKronecker tensor product矩阵的AB. IfAis anm-by-nmatrix andBis ap-by-q然后kron(A,B)is anm*p-by-n*qmatrix formed by taking all possible products between the elements ofA和the matrixB.

Examples

collapse all

Create a block diagonal matrix.

创建一个4 x-4的身份矩阵和一个2 by-2矩阵,您希望沿对角线重复。

一个=眼(4);B = (1 -1;-1 1];

Usekronto 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.

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.

This example visualizes a sparse Laplacian operator matrix.

The matrix representation of the discrete Laplacian operator on a two-dimensional,n-by-ngrid is an*n-by-n*nsparse 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')

Figure contains an axes object. The axes object contains an object of type line.

Input Arguments

collapse all

Input matrices, specified as scalars, vectors, or matrices. If eitherA或者B稀疏,然后kronmultiplies 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

collapse all

Kronecker Tensor Product

IfAis anm-by-nmatrix andBis ap-by-q然后the Kronecker tensor product ofABis a large matrix formed by multiplyingBby each element ofA

A B = [ a 11 B a 12 B a 1 n B a 21 B a 22 B a 2 n B a m 1 B a m 2 B a m n B ] .

For example, two simple 2-by-2 matrices produce

A = [ 1 2 1 0 ] , B = [ 4 3 2 3 ] A B = [ 1 · 4 1 · 3 2 · 4 2 · 3 1 · 2 1 · 3 2 · 2 2 · 3 1 · 4 1 · 3 0 · 4 0 · 3 1 · 2 1 · 3 0 · 2 0 · 3 ] = [ 4 3 8 6 2 3 4 6 4 3 0 0 2 3 0 0 ] .

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a