repeated data in matlab

To repeat data in MATLAB, the repmat() function can be used. The basic syntax for repmat() function is as follows:

main.m
B = repmat(A, m, n)
20 chars
2 lines

In the above syntax, A is the array or matrix that you want to repeat, and m and n are the number of times you want to repeat A along the rows and columns, respectively. The output B is a matrix of size (size(A,1)*m, size(A,2)*n).

Here's an example to repeat a row vector [1 2 3] three times horizontally and two times vertically:

main.m
>> A = [1 2 3];
>> B = repmat(A, 2, 3)

B =

     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3
375 chars
12 lines

In the above example, A is repeated twice along the rows and three times along the columns. The output B is a matrix of size 6x9.

Note: If you want to repeat an array or matrix multiple times with the same size, you can also use the colon notation A(:) to create a column vector, and then repeat it using repmat().

related categories

gistlibby LogSnag