# MATLAB repelem() Function

This tutorial will discuss repeating copies of array elements using MATLAB’s `repelem()`

function.

## MATLAB `repelem()`

Function

Manually creating an array of many elements takes a lot of time because we have to write each array element. But we can use loops and some built-in functions of Matlab to create an array of many elements in a short time.

The `repelem()`

function is also used to create arrays of a large number of elements in a short time by repeating a scalar or a vector. The `repelem()`

function is used to repeat copies of array elements and create a new array or matrix from the repeated elements.

The `repelem()`

function has two syntaxes, and the first syntax is shown below.

```
output = repelem(v,n)
```

The first syntax will return a vector of repeated elements of vector or scalar `v`

. If `n`

is a scalar, the input vector `v`

values will be repeated `n`

times, and the length of the output vector will be equal to the length of the input vector multiplied by the `n`

.

If `n`

is a vector, each element of the vector `v`

will be repeated according to the corresponding element of the vector `n`

, and vector `n`

should have the same length as the input vector `v`

. The output vector’s length will equal the sum of the vector `n`

elements.

In other words, the first element of vector `v`

will be repeated according to the first element of vector `n`

and so on. For example, let’s create a vector and repeat it to create two new vectors.

See the code below.

```
clc
clear
X = [1 2 3]
Y = repelem(X,2)
Z = repelem(X,[1 2 3])
```

Output:

```
X =
1 2 3
Y =
1 1 2 2 3 3
Z =
1 2 2 3 3 3
```

In the above code, we repeated each element of the vector `X`

two times to create the new vector `Y`

. We repeated the first element of vector `X`

one time, the second element of vector `X`

two times, and the third element three times to create the new vector `Z`

.

We can see in the output that each element of vector `X`

is repeated two times to create the vector `Y`

, and the vector `Z`

shows that it is created according to the vector defined as the second argument in the second `repelem()`

function.

The second syntax of the `repelem()`

function is shown below:

```
output = repelem(m,r1,...,rN)
```

The second syntax is used to repeat elements of a matrix to create a new 2D or 3D matrix and array. The first argument `m`

is the input matrix or array, which can be of any dimension.

All other arguments are used to set the block size, which is used to repeat each element of the input matrix or array, and it can be a scalar or a vector of the same length as the specific dimension of the input matrix. For example, if we pass 2 and 3 as the second and third arguments inside the `repelem()`

function, it will create a 2-by-3 matrix by repeating each element of the input matrix and placing it in the new matrix.

In other words, each element of the input matrix will be repeated two times in the row or first dimension and 3 times in the column or second dimension to create the new array or matrix. If we pass the fourth argument, each element of the input matrix will also be repeated in the third dimension to create a multidimensional array or matrix.

For example, let’s create a 2-by-2 matrix and repeat its elements to create a new matrix. See the code below.

```
clc
clear
X = magic(2)
Y = repelem(X,2,2)
```

Output:

```
X =
1 3
4 2
Y =
1 1 3 3
1 1 3 3
4 4 2 2
4 4 2 2
```

In the above code, we repeated each element of the input matrix two times in the first or row dimension and two times in the second or column dimension to create the new matrix. The output matrix’s size will equal the multiplication of specific dimensions of the input vector and the block size.

In the above example, the size of the output matrix will be 4-by-4 because the input matrix size is 2-by-2, and the block size is also 2-by-2. The dimensions of the output matrix will be equal to the dimension of the block size like in the above example, the block has two dimensions, and the output matrix also has two dimensions.

If we don’t want to repeat an element in a specific dimension, we can pass one as the value of that dimension. For example, if we don’t want to repeat the elements in the first or row dimension so that the number of rows should be the same, we can pass one as the second argument in the above code.

In the above example, each element will be repeated 2 times in both dimensions. But if we want to repeat each element according to our requirements, we can use a vector of the same length as the specific dimension, which will define the number of times the specific elements will be repeated.

For example, let’s repeat the above example and repeat only column values according to a vector. See the code below.

```
clc
clear
X = magic(2)
Y = repelem(X,1,[3 2])
```

Output:

```
X =
1 3
4 2
Y =
1 1 1 3 3
4 4 4 2 2
```

In the above code, we are repeating the first column of the input matrix 3 times and the second column 2 times to create a new matrix `Y`

. We can see in the above output that the number of rows of the output matrix is the same as the number of rows of the input matrix because we have not repeated the rows, and the number of columns has increased because we repeated the first column three times and the second column 2 times.

We can do this operation with any dimension. We can also use the `repelem()`

function to create a large table from a small table by repeating its values in specific dimensions.

For example, let’s create a table using the `table()`

function and then create another large table by repeating the values of the first table. See the code below.

```
clc
clear
T_1 = table([19; 16],[60; 53],'VariableNames',{'Age' 'Height'})
T_2 = repelem(T_1, 2,2)
```

Output:

```
T_1 =
2×2 table
Age Height
___ ______
19 60
16 53
T_2 =
4×4 table
Age Age_1 Height Height_1
___ _____ ______ ________
19 19 60 60
19 19 60 60
16 16 53 53
16 16 53 53
```

In the above code, we repeated the row and the column dimension 2 times to create the larger table.