repeat

repeat(A, inner = Int[], outer = Int[])

Construct an array by repeating the entries of A. The i-th element of inner specifies the number of times that the individual entries of the i-th dimension of A should be repeated. The i-th element of outer specifies the number of times that a slice along the i-th dimension of A should be repeated.

Examples

  1. Repeat elements of an array:

    julia> A = [1, 2, 3];
    julia> repeat(A, inner = [2])
    6-element Array{Int64,1}:
    1
    1
    2
    2
    3
    3

    This example repeats each element of array A twice.

  2. Repeat rows of a matrix:

    julia> A = [1 2; 3 4];
    julia> repeat(A, outer = [2])
    4×2 Array{Int64,2}:
    1  2
    1  2
    3  4
    3  4

    It repeats each row of matrix A twice.

  3. Repeat elements of a multi-dimensional array:
    julia> A = [1 2; 3 4];
    julia> repeat(A, inner = [1, 2], outer = [2])
    4×4 Array{Int64,2}:
    1  2  1  2
    3  4  3  4
    1  2  1  2
    3  4  3  4

    This example repeats each element of A along the second dimension twice and repeats the entire matrix twice along the first dimension.

Common mistake example:

julia> A = [1, 2, 3];
julia> repeat(A, inner = [2, 3])
ERROR: DimensionMismatch("repeat dimensions must be either 1 or equal to size(A)")

Here, the inner parameter specifies a different number of repeats for each dimension, resulting in a DimensionMismatch error. The inner and outer dimensions should either be of size 1 or match the corresponding dimensions of A to avoid this error.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: