HOWTO · MATLAB

MATLAB Repelem() Function

Use MATLAB repelem to repeat scalar and array elements with uniform or per-element counts.

On this page

repelem creates a new array by placing adjacent copies of each input element. Use it to expand a scalar, repeat every element by the same count, or set counts for individual elements along one or more dimensions. The result is a new array, so estimate its size before applying large repetition counts.

Understand repelem syntax and repetition behavior

The basic form is B = repelem(A,r). For a scalar repetition count, MATLAB repeats each element of A that many times. For a vector of counts, each count applies to the corresponding element along the dimension being expanded. The count vector must have one entry for each input element along that dimension.

For arrays, provide a repetition argument for each dimension you want to control: B = repelem(A,r1,r2,...,rN). A scalar argument repeats each element uniformly along its dimension; a count vector sets per-element repetitions along that dimension. Use 1 for a dimension whose elements should not be repeated. The size of the result in a dimension is the sum of its repetition counts.

Repeat scalar and vector elements

A scalar input becomes a vector containing the requested number of copies. For a vector, a scalar count applies uniformly, while a count vector gives each element its own number of copies. For example, with A = [1 2 3], the call repelem(A,2) repeats all three values twice. The call repelem(A,[1 2 3]) keeps the first value once, repeats the second twice, and the third three times. The output length is the sum of the counts.

Run this example:

x = [1 2 3];
y = repelem(x, 2);
disp(y);

The result contains two adjacent copies of each input value, in the original order. This is a self-contained Octave example of the same repelem operation.

Repeat array elements along selected dimensions

For a matrix, repetition arguments correspond to rows first and columns second. For example, repelem(A,2,2) doubles the number of rows and columns by repeating every element in both directions. With A = magic(2), the input is [1 3; 4 2], so repeating each element twice in each dimension produces rows [1 1 3 3], [1 1 3 3], [4 4 2 2], and [4 4 2 2].

Per-element counts work the same way. In repelem(A,1,[3 2]), the row count of 1 leaves each row unchanged, and [3 2] repeats the first and second columns three and two times. The result has two rows and five columns. For a 2-by-2 matrix, repelem(A,[2 1],[3 2]) produces three rows and five columns: the first input row is repeated twice, the second once, and the columns are expanded according to their counts.

Repeat table values

repelem is described here for scalar and array inputs. If your data is in a MATLAB table, select the variable whose values you want to repeat, then apply repelem to that variable. For example, repelem(T.Age,2) expresses repeating each value in the Age variable twice. This operates on the variable’s values; it does not establish that repelem duplicates table columns or constructs a larger table. Check the behavior in the MATLAB release and table workflow you use before relying on table-level expansion.

Check output size and memory use

Calculate the output dimensions before running a large expansion. With per-element counts, sum the counts for each expanded dimension; with a uniform count, multiply that dimension’s size by the count. The total number of output elements is the product of the resulting dimensions, and the array’s data type determines the storage needed for those elements. A large result can require substantially more memory than the input and may exceed available memory. Reduce counts or process data in smaller portions when the full expanded array is unnecessary.