repmat — Tile arrays across one or more dimensions using MATLAB-compatible repmat rules.
repmat(A, reps) tiles A according to replication factors in reps. It follows MATLAB-compatible shape rules and supports numeric, logical, char, string, cell, and GPU-backed array inputs.
Syntax
B = repmat(A, r)
B = repmat(A, m, n)
B = repmat(A, d1, d2, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input array to replicate. |
r | Any | Yes | — | Replication vector or scalar factor. |
m | NumericScalar | Yes | — | Row replication factor. |
n | NumericScalar | Yes | — | Column replication factor. |
dims | NumericScalar | Variadic | — | Per-dimension replication factors. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Replicated array tiled by requested replication factors. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:repmat:MissingReplicationFactors | No replication factors are provided. | repmat: replication factors must be specified |
RunMat:repmat:InvalidReplicationFactors | Replication factors are non-scalar, non-integer, negative, or otherwise invalid. | repmat: invalid replication factors |
RunMat:repmat:UnsupportedInput | Input type is unsupported for repmat. | repmat: unsupported input type |
RunMat:repmat:Internal | Internal allocation/indexing/provider path fails. | repmat: internal operation failed |
How repmat works
repmat(A, m, n)orrepmat(A, [m n])repeatsAmtimes along rows andntimes along columns.- A size vector
[r1 r2 … rN]can be given as a row or column; trailing dimensions default to1, letting you replicate only the axes you care about. - Supplying a scalar replication factor (e.g.,
repmat(A, k)) duplicatesAacross every dimension, ensuring the first two dimensions are replicated even when the input is a row vector. - Zero replication factors produce empty dimensions, e.g.,
repmat(A, 0, 3)yields an empty array with shape[0 size(A,2)]. - Char and cell arrays follow MATLAB by supporting row/column tiling; additional dimensions must be
1, while numeric, logical, complex, and string arrays support full N-D replication. - Replication factors must be finite, non-negative integers; RunMat raises descriptive errors for negative, fractional, or excessively large sizes.
- GPU arrays remain on the device when the acceleration provider implements the tiling hook; otherwise, RunMat gathers to host memory, tiles in software, and re-uploads the replicated tensor so downstream GPU work keeps residency.
Does RunMat run repmat on the GPU?
RunMat first calls AccelProvider::repmat, giving the backend a chance to tile directly on the device. Providers that have not yet implemented this hook fall back to a safe path that gathers the tensor, performs tiling on the host, and uploads the replicated data back to the GPU. The fallback preserves correct behaviour today while enabling backend authors to drop in optimized kernels as they become available.
GPU memory and residency
You usually do not need to call gpuArray directly. RunMat's planner keeps values on the GPU when it detects that further operations benefit from staying there. Explicit gpuArray calls remain available for compatibility with legacy MATLAB code and when you want to steer residency manually.
Examples
Tiling a matrix across rows and columns
A = [1 2; 3 4];
B = repmat(A, 2, 3)Expected output:
B =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4Using a scalar replication factor
row = 1:4;
Tiled = repmat(row, 3);
size(Tiled)Expected output:
ans =
3 12Replicating into three dimensions
A = reshape(1:6, [1 3 2]);
T = repmat(A, [2 1 4]);
size(T)Expected output:
ans =
2 3 8Repeating logical masks with zero dimensions
mask = logical([1 0 1]);
emptyMask = repmat(mask, 0, 3);
size(emptyMask)Expected output:
ans =
0 3Replicating string scalars into string arrays
name = "runmat";
names = repmat(name, 2, 2)Expected output:
names =
2x2 string array
"runmat" "runmat"
"runmat" "runmat"Replicating data that lives on the GPU
G = gpuArray(magic(3));
T = repmat(G, [2 1]);
result = gather(T)Using repmat with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how repmat changes the result.
Run a small repmat example, explain the result, then change one input and compare the output.
FAQ
Are replication factors required to be integers?⌄
Yes. RunMat follows MATLAB and requires every replication factor to be a non-negative integer. Non-integers raise the descriptive error repmat: replication factor <n> must be an integer.
What happens when I pass a scalar replication factor?⌄
RunMat duplicates the input along every dimension, ensuring at least the first two dimensions receive the factor so matrices tile both rows and columns.
Can I request zero replication along a dimension?⌄
Yes. Zero factors produce empty dimensions while preserving the remaining sizes, which is useful when constructing placeholder tensors or short-circuiting loops.
Does repmat work with char, string, or cell arrays?⌄
Yes. Char arrays and cell arrays tile across rows and columns (extra dimensions must currently be 1), while string arrays, numeric tensors, logical arrays, and complex tensors support full N-D replication.
How does repmat handle GPU tensors today?⌄
The runtime asks the provider for a device implementation. If none exists (for example, the in-process test provider), RunMat gathers to the host, tiles there, and re-uploads the result so downstream GPU kernels still see the replicated tensor.
Does the result reuse backing storage from the input?⌄
No. repmat always creates a new array so modifying the result never mutates the original input.
Can replication overflow memory?⌄
RunMat checks for overflow when multiplying shape dimensions. If the requested size cannot fit in native address space, the builtin raises a descriptive error before attempting allocation.
Does repmat preserve data ordering?⌄
Yes. Column-major ordering is maintained for numeric, logical, string, and complex arrays, while char and cell arrays respect their row-major storage conventions within RunMat.
Related Array functions
Shape
cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · reshape · rot90 · squeeze · tril · triu · vertcat
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how repmat is executed, line by line, in Rust.
- View the source for repmat in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.