Written and maintained by CASRAI Editorial Board
Last updated
Researchers and analysts searching for repmat in MATLAB usually need to tile a scalar, vector or matrix into a larger array and predict the resulting size. repmat “repeats copies of an array” (MathWorks reference, R2026b). The size rule is short: for a matrix, size(B) = size(A) .* [r1 ... rN]. This page gives the syntax, the size arithmetic with worked sizes, the error and empty-array rules, and how repmat relates to repelem and NumPy tile.
What repmat is used for in research
MATLAB repmat builds a new array by repeating copies of an input array along each dimension. Typical analysis uses are stacking a row vector into a matrix, building a constant matrix, and tiling a small block across a larger grid. The MathWorks reference lists the input A as a scalar, vector, matrix or N-D array, and says it also supports table, cell, struct, categorical and datetime arrays, and complex values. The function was introduced before R2006a.
For related array work see the CASRAI guide to the matrix in MATLAB and the guide to the MATLAB data table.
Access and syntax (official source only)
The authoritative reference is the MathWorks documentation page for repmat (https://www.mathworks.com/help/matlab/ref/repmat.html, read 2026-11-07, release R2026b). It gives three call forms:
B = repmat(A,n)repeatsncopies in the row and column dimensions.B = repmat(A,r1,...,rN)repeatsr1copies along the first dimension,r2along the second, and so on.B = repmat(A,r)takes the repetition factors as a vector.repmat(A,[2 3])is the same asrepmat(A,2,3).
The repetition arguments (n, r1 to rN, r) are integers.
Step by step: tile an array and predict its size
- Pick the block. Let
Abe the array to repeat. - Choose the factors. For one factor per dimension use
repmat(A,r1,r2)orrepmat(A,[r1 r2]). - Predict the size.
size(B)equalssize(A) .* [r1 r2 ...]. With the single-argument formrepmat(A,n)the size of a matrix result issize(A)*n. - Run it and check. Call
size(B)and compare it with your prediction.
Worked examples (from the MathWorks reference)
| Code | Input | Result size |
|---|---|---|
A = repmat(10,3,2) |
scalar 10 | 3-by-2 (every element 10) |
B = repmat(A,2) |
A = diag([100 200 300]) (3-by-3) |
6-by-6 |
B = repmat(A,2,3) |
A = diag([100 200 300]) |
6-by-9 |
B = repmat(A,[2 3 2]) |
A = [1 2;3 4] |
4-by-6-by-2 |
B = repmat(A,4,1) |
A = 1:4 (row vector) |
4-by-4 (row stacked four times) |
B = repmat(A,1,4) |
A = (1:3)' (column vector) |
3-by-4 (column repeated four times) |
Tables: repeated variable names are numbered
When A is a table, repmat numbers the copies of the variable names. For a table with variables Age and Height and two rows, repmat(A,2,3) returns a 4-by-6 table whose variables are Age, Height, Age_1, Height_1, Age_2 and Height_2.
Output and how to read it
The output B has a size scaled per dimension by the factors, and copies of A tile along each dimension. A 0 or negative repetition factor does not error: it returns an empty array.
Every pair of elements (repmat with repelem)
The reference gives a combination idiom: T = [repelem(A,numel(B)) repmat(B,numel(A),1)] pairs every element of A with every element of B. It also points to combinations(A,B), available from R2023a, which returns a table.
Troubleshooting
| Symptom | Cause and fix (per the reference) |
|---|---|
| Empty result | A factor of 0 or a negative integer returns an empty array. Check the factors. |
| Error after upgrading to R2019b or later | From R2019b, nonscalar or empty repetition arguments error. Replace repmat(A,r1,r2) where r1 and r2 are row vectors with repmat(A,[r1 r2]); replace repmat(A,empt) with repmat(A,1); replace repmat(A,n,empt) with repmat(A,[n 1]). |
| Tall array fails | For tall arrays the repetition factor of the first dimension must be 1. |
| Code generation (MATLAB Coder, GPU Coder) | The size arguments must be fixed-size. |
repmat and related functions
The reference See Also list for repmat includes bsxfun, kron, repelem, reshape, resize, paddata, meshgrid and ndgrid. In the pairing idiom above, repmat repeats the whole array while repelem repeats each element, so the two are used together. For plotting arrays you build, see the guide to MATLAB graph plots.
Free alternatives
| Tool | Equivalent | Behaviour (per its documentation) |
|---|---|---|
| NumPy (Python, open source) | numpy.tile(A, reps) |
“Construct an array by repeating A the number of times given by reps.” If A.ndim is greater than len(reps), reps is promoted by prepending 1s. np.tile(np.array([0,1,2]),(2,2)) gives [[0,1,2,0,1,2],[0,1,2,0,1,2]]. |
For other NumPy array operations see NumPy sum: np.sum syntax, axis, NaN and dtype.
FAQ
What does repmat do in MATLAB?
It repeats copies of an array. repmat(A,2,3) tiles A two times down and three times across, so a 3-by-3 matrix becomes 6-by-9.
Is repmat(A,[2 3]) the same as repmat(A,2,3)?
Yes. The reference states that repmat(A,r) with r = [2 3] equals repmat(A,2,3).
How do I stack a row vector into a matrix?
Use repmat(A,4,1); with A = 1:4 the result is 4-by-4.
What happens with a zero or negative factor?
The result is an empty array.
Sources
- MathWorks, “repmat” (MATLAB documentation, R2026b): https://www.mathworks.com/help/matlab/ref/repmat.html, read 2026-11-07.
- NumPy, “numpy.tile”: https://numpy.org/doc/stable/reference/generated/numpy.tile.html, read 2026-11-07.







