kron — Compute Kronecker products in MATLAB and RunMat.
kron(A, B) forms the Kronecker (tensor) product of A and B. Output sizing and column-major block ordering follow MATLAB semantics.
Syntax
C = kron(A, B)Inputs
A- Type
Any- Required
- Yes
- Description
- Left numeric, logical, character, or sparse input.
B- Type
Any- Required
- Yes
- Description
- Right numeric, logical, character, or sparse input.
Returns
C- Type
Any- Description
- Kronecker product of A and B.
Errors
RunMat:kron:TooManyInputs- When
- Extra arguments were supplied after A and B.
- Message
kron: too many input arguments
RunMat:kron:UnsupportedInput- When
- Input values are not supported numeric, logical, character, or sparse arrays.
- Message
kron: unsupported input type
RunMat:kron:Internal- When
- Internal conversion, allocation, or provider path fails.
- Message
kron: internal operation failed
Inputs and output
Scalars, vectors, and matrices are supported. An operand with more than two dimensions is a separately gated RunMat extension.
Each output extent is the checked product of the corresponding input extents; zero extents remain empty. A scalar operand scales the other operand.
Complex operands produce complex output. Logical and character operands are promoted to double precision before multiplication.
All eight real integer classes are supported: matching integer classes multiply with saturation, or a scalar double may scale an integer operand. Complex integer arithmetic and other mixed integer-array classes are rejected.
Sparse input uses the CSC Kronecker implementation and preserves sparse output without a dense conversion. Explicit device intent cannot publish a sparse host result.
GPU and fusion
Compatible resident floating operands use the owning provider's kron hook when available. Other supported forms gather authoritative data, compute on the host, and restore a class-preserving result to the source provider when possible.
Explicit gpuArray inputs either return a resident result or raise an error. The operation is a fusion boundary; no elementwise fusion kernel is claimed.
Automatic offload may keep compatible values resident without an explicit gpuArray call. Provider outputs are checked for shape, owner, device, storage, precision, and non-aliasing before publication.
Examples
Computing the Kronecker product of two matrices
A = [1 2; 3 4];
B = [0 5; 6 7];
C = kron(A, B)Expected output:
C =
0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28Kronecker product with row and column vectors
row = [1 2 3];
col = (1:3)';
K = kron(row, col);
size(K)Expected output:
ans =
3 9Scaling a matrix with a scalar
A = [1 2; 3 4];
S = kron(2, A)Expected output:
S =
2 4
6 8Building block-diagonal systems
I = eye(3);
M = [1 0; 0 -1];
blockDiag = kron(I, M)Kronecker product of complex matrices
A = [1+2i 0; 0 3-1i];
B = [0 1; 2 3i];
C = kron(A, B)Using logical masks
mask = logical([1 0; 0 1]);
tile = kron(mask, ones(2))Running kron on GPU-resident tensors
G = gpuArray(rand(2));
H = gpuArray([1 -1; -1 1]);
K = kron(G, H);
isgpuarray(K)Expected output:
ans = logical 1Using kron with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how kron changes the result.
Run a small kron example, explain the result, then change one input and compare the output.
FAQ
When should I use kron instead of matrix multiplication?⌄
Use kron for block matrices built from every pair of operand elements; matrix multiplication contracts dimensions.
Does kron preserve sparsity?⌄
Yes. The current CSC path multiplies stored entries and returns sparse output; it does not densify sparse operands.
Can I mix real and complex inputs?⌄
Yes. Either complex operand produces complex output with the corresponding real and imaginary arithmetic.
What happens with logical inputs?⌄
Logical values become double-precision zeroes and ones before multiplication.
Are character arrays supported?⌄
Yes. Characters are converted to their Unicode code points in double precision.
How big can the result be?⌄
Output dimensions and element count are checked for overflow before allocation.
Does the GPU path always stay on-device?⌄
Compatible floating inputs use an owning-provider hook; otherwise a supported fallback gathers and restores when class and ownership permit. Explicit gpuArray inputs never silently become host results.
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how kron is executed, line by line, in Rust.
- View the source for kron in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.