repelem — Repeat each element of an array by per-dimension replication factors, matching MATLAB repelem semantics.
repelem(A, r1, r2, ..., rN) repeats each element of A along the requested dimensions. Unlike repmat, which tiles whole arrays, repelem expands individual elements into repeated runs or blocks.
Syntax
B = repelem(A, R)
B = repelem(A, R1, R2, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input array. |
R | Any | Yes | — | Scalar or vector replication control argument. |
R1_RN | Any | Variadic | — | Per-dimension scalar/vector replication controls. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Array with element-wise replication applied. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:repelem:MissingReplicationFactors | No replication arguments are provided. | repelem: replication factors must be specified |
RunMat:repelem:InvalidReplicationFactors | Replication arguments are non-numeric, non-integer, or shape-incompatible. | repelem: invalid replication factors |
RunMat:repelem:UnsupportedInput | Input type is unsupported for repelem. | repelem: unsupported input type |
RunMat:repelem:Internal | Internal allocation/indexing path fails. | repelem: internal operation failed |
How repelem works
repelem(v, n)repeats each element of a row or column vectorvntimes and preserves vector orientation.repelem(v, counts)accepts a count vector with one non-negative integer count per element ofv.repelem(A, r1, r2, ..., rN)repeats matrix or N-D array elements along each dimension; scalar factors create element blocks and vector factors provide per-index counts for that dimension.- Numeric, logical, complex, string, char, and cell inputs are supported. Numeric, logical, complex, string, and cell arrays support N-D replication; char arrays are represented as 2-D character matrices.
- Zero counts are allowed and remove the corresponding element or dimension slice from the output.
- Replication counts must be finite, non-negative integers. Count vectors must match the size of the dimension they apply to.
- The builtin checks shape and size overflow before materializing output arrays.
Extended capabilities
repelempreserves the input value class for numeric dtype metadata, logical arrays, complex tensors, string arrays, char arrays, and cell arrays.- N-D numeric, logical, complex, string, and cell replication uses the runtime's native storage order for each value kind.
Does RunMat run repelem on the GPU?
RunMat does not yet expose a provider hook for element-wise replication. Backends can add one later without changing the MATLAB-facing behavior.
GPU memory and residency
repelem is currently a host-resident array construction builtin. If a GPU tensor reaches this builtin, RunMat gathers it before replication.
Examples
Repeating every element of a row vector
u = repelem([1 2 3], 2)Expected output:
u =
1 1 2 2 3 3Using a per-element count vector
u = repelem([1 2 3], [1 2 3])Expected output:
u =
1 2 2 3 3 3Expanding a matrix into element blocks
A = [1 2; 3 4];
B = repelem(A, 2, 3)Expected output:
B =
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4Dropping elements with zero counts
u = repelem([1 2 3 4 5], [0 1 0 2 1])Expected output:
u =
2 4 4 5Repeating along a higher dimension
C = {1; 2};
D = repelem(reshape(C, [1 1 2]), 1, 1, 2);
size(D)Expected output:
ans =
1 1 4Using repelem with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how repelem changes the result.
Run a small repelem example, explain the result, then change one input and compare the output.
FAQ
How is repelem different from repmat?⌄
repmat tiles a whole array. repelem repeats each individual element, so matrix inputs expand into element-wise blocks.
Can counts be different for each element?⌄
Yes. In vector form, the count vector must have one entry for each vector element. In multi-dimensional form, a count vector applies to the dimension at the same argument position.
Are zero counts valid?⌄
Yes. A zero count omits that element or dimension index from the output.
Does repelem preserve row and column vector orientation?⌄
Yes. Row vectors remain row vectors, column vectors remain column vectors, including empty column vectors.
Does repelem run on the GPU?⌄
Not yet. The runtime gathers GPU tensors to host memory for this builtin, computes the replicated result on the CPU, and returns a host value.
Related Array functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how repelem is executed, line by line, in Rust.
- View the source for repelem 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.