Language Reference


REPEAT Function

REPEAT (x, nrow, ncol); REPEAT (x, freq);

The REPEAT function creates a matrix of repeated values. There are two ways to specify the syntax. The first syntax repeats the entire matrix nrow*ncol times. The arguments for this syntax are as follows:

x

is a numeric matrix or literal.

nrow

specifies the number of times matrix is repeated down rows.

ncol

specifies the number of times matrix is repeated across columns.

The REPEAT function creates a new matrix by repeating the values of the argument matrix nrow*ncol times: ncol times across the rows, and nrow times down the columns. The matrix argument can be numeric or character. For example, the following statements form a new matrix that consists of two vertical and three horizontal copies of x:

x = {1 2,
     3 4};
y = repeat(x, 2, 3);
print y;

Figure 24.324: Repeated Values

y
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4



A second way to call the REPEAT function is to provide an argument, freq that has the same number of elements as x. The return value is a row vector in which x[1] is repeated freq[1] times, x[2] is repeated freq[2] times, and so forth, where the elements of x are enumerated in row-major order. Each element of freq should be a nonnegative integer. The return value will have sum(freq) elements. This is shown in the following example:

z = repeat(x, {2 3 0 1});
print z;

Figure 24.325: Repeated Values from a Frequency Vector

z
1 1 2 2 2 4