Skip to main content
v2026.11,858 entries · CC-BY 4.0

repmat in MATLAB: Syntax, Examples and Size Rules

How MATLAB repmat tiles arrays: syntax, size arithmetic with worked examples, R2019b error rules and the NumPy tile equivalent.

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) repeats n copies in the row and column dimensions.
  • B = repmat(A,r1,...,rN) repeats r1 copies along the first dimension, r2 along the second, and so on.
  • B = repmat(A,r) takes the repetition factors as a vector. repmat(A,[2 3]) is the same as repmat(A,2,3).

The repetition arguments (n, r1 to rN, r) are integers.

Step by step: tile an array and predict its size

  1. Pick the block. Let A be the array to repeat.
  2. Choose the factors. For one factor per dimension use repmat(A,r1,r2) or repmat(A,[r1 r2]).
  3. Predict the size. size(B) equals size(A) .* [r1 r2 ...]. With the single-argument form repmat(A,n) the size of a matrix result is size(A)*n.
  4. 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.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about repmat in MATLAB: Syntax, Examples and Size Rules

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →