Matlab

# How to Use repmat Function in MATLAB

Initially, the main purpose of designing MATLAB was to perform array operations. MATLAB includes many built-in functions to perform complex array operations. One such array operation is a manipulation of an array by repeating its elements using the repmat() function.

This blog is going to discover how to use the repmat() function in MATLAB.

## How to Use the repmat() Function in MATLAB?

The repmat() is a built-in function in MATLAB that allows us to repeat elements of an array for the desired time. The repetition of the array of elements depends on the specified parameters in the function. This function accepts an array and the number of repetitions as the arguments and returns a new array that is an expansion of the original array containing the copies of the original array.

## Syntax

The repmat() function uses different syntaxes in MATLAB, which are given below:

B = repmat(A,n)

B = repmat(A,r1,...,rN)

B = repmat(A,r)

Here:

The function B = repmat(A,n) creates the n copies of the original array A in the row and column dimensions. It creates a matrix B having a size equal to size(A)*n where A is a matrix.

The function B = repmat(A,r1,…, rN) creates the [r1,r2â€¦rN] copies of the original array A in the specified r1,r2â€¦rN dimensions. It creates an array B having the dimension size(A)*[r1,r2â€¦rN].

The function B = repmat(A,r) specifies the repetition of the original array in the dimension specified by the row vector r. For example, if r= [2 4], array B will have 2-row copies and 4-column copies of the original array A.

## Examples

Follow the given examples for more understanding of the repmat() function in MATLAB.

## Example 1: How to Convert a Vector to a Matrix Using the repmat(A,n) Function?

In this example, we create a vector x and use the repmat() function to repeat all elements of x in the dimension n=2. As a result, we obtain a matrix y having dimension 2-by-8.

x = 1:4;

y = repmat(x,2)

## Example 2: How to Convert a Matrix to an Array Using the repmat(A,r1,r2,â€¦rN) Function?

This example creates a matrix A having dimension 2-by-3 and uses the repmat() function to repeat all elements of A in dimension 2-by-3-by-2. As a result, we obtain an array B having dimensions 4-by-9-by-2.

A = [1:3;4:6];

B = repmat(A,2,3,2)

## Example 3: How to Expand a Matrix Using the repmat(A,r) Function?

This MATLAB code creates a matrix A having dimension 2-by-3 and uses the repmat() function to repeat all elements of A in dimension r. As a result, we obtain a matrix B having dimension 6-by-6.

A = [1:3;4:6];

r = [3 2];

B = repmat(A, r)

## Conclusion

MATLAB allows us to perform array operations using various built-in functions. One of those functions is the repmat() function which enables us to repeat the copies of the original array in the specified dimension. This tutorial has provided a brief guide on how to use the repmat() function in MATLAB using some examples. This allows the beginner to learn the skill of using this function in MATLAB programming.