Documentation

repmat

Replicate and tile array

Syntax

Description

example

B = repmat(A,n) returns an n-by-n tiling of A. The size of B is size(A) * n

example

B = repmat(A,sz1,sz2,...,szN) specifies a list of scalars, sz1,sz2,...,szN, to describe an N-D tiling of A. The size of B is [size(A,1)*sz1, size(A,2)*sz2,...,size(A,n)*szN]. For example, repmat([1 2; 3 4],2,3) returns a 4-by-6 matrix.

example

B = repmat(A,sz) specifies a row vector, sz, instead of a list of scalars, to describe the tiling of A. This syntax returns the same output as the previous syntax. For example, repmat([1 2; 3 4],[2 3]) returns the same result as repmat([1 2; 3 4],2,3).

Examples

expand all

Replicate and Tile a Matrix

Create a diagonal matrix.

A = diag([100 200 300])
A =

   100     0     0
     0   200     0
     0     0   300

Create a 2-by-2 tiling of A.

B = repmat(A,2)
B =

   100     0     0   100     0     0
     0   200     0     0   200     0
     0     0   300     0     0   300
   100     0     0   100     0     0
     0   200     0     0   200     0
     0     0   300     0     0   300

Replicate and Tile an N-D Array

Create a 2-by-2-by-2 array.

A = zeros(2,2,2);
A(:,:,1) = diag([10 20]);
A(:,:,2) = diag([55 99]);
A
A(:,:,1) =

    10     0
     0    20


A(:,:,2) =

    55     0
     0    99

Create a 3-by-3 tiling of A.

B = repmat(A,3)
B(:,:,1) =

    10     0    10     0    10     0
     0    20     0    20     0    20
    10     0    10     0    10     0
     0    20     0    20     0    20
    10     0    10     0    10     0
     0    20     0    20     0    20


B(:,:,2) =

    55     0    55     0    55     0
     0    99     0    99     0    99
    55     0    55     0    55     0
     0    99     0    99     0    99
    55     0    55     0    55     0
     0    99     0    99     0    99

Vertical Stack of Row Vectors

Create a row vector.

A = 1:4
A =

     1     2     3     4

Create a vertical stack of four copies of A.

B = repmat(A,4,1)
B =

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

B contains 4 copies of A in the first dimension and 1 copy in the second dimension. This result is equivalent to B = repmat(A,[4 1]).

Horizontal Stack of Column Vectors

Create a column vector.

A = (1:3)'
A =

     1
     2
     3

Create a horizontal stack of four copies of A.

B = repmat(A,1,4)
B =

     1     1     1     1
     2     2     2     2
     3     3     3     3

B contains 1 copy of A in the first dimension and 4 copies in the second dimension. This result is equivalent to B = repmat(A,[1 4]).

Replicate and Tile a Complex Scalar Value

Define a complex scalar value.

A = 5+1i;

Create a 3-by-2 tiling of A.

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

   5.0000 + 1.0000i   5.0000 + 1.0000i
   5.0000 + 1.0000i   5.0000 + 1.0000i
   5.0000 + 1.0000i   5.0000 + 1.0000i

B contains 3 copies of A in the first dimension and 2 copies in the second dimension. This result is equivalent to B = repmat(A,3,2).

Replicate and Tile a Cell Array

Create a cell array containing a string and numeric values.

A = {'Values'; 10; 105}
A = 

    'Values'
    [ 10]
    [105]

Create a 3-by-2 tiling of A.

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

    'Values'    'Values'
    [    10]    [    10]
    [   105]    [   105]
    'Values'    'Values'
    [    10]    [    10]
    [   105]    [   105]
    'Values'    'Values'
    [    10]    [    10]
    [   105]    [   105]

B contains 3 copies of A in the first dimension and 2 copies in the second dimension. This result is equivalent to B = repmat(A,3,2).

Input Arguments

expand all

A — Input arrayscalar | vector | array | cell array | table | structure | object

Input array, specified as a scalar, vector, array, cell array, table, structure, or object.

Example: 1

Example: [1 2 3]

Example: [1 2; 3 4]

Example: {'foo',[20 30 40]}

Example: table(rand(5,1),rand(5,1))

Example: struct('Age',25,'Weight',110)

n — Uniform tiling factorscalar

Uniform tiling factor, specified as a scalar value. The n argument creates an n-by-n tiling of A. If n is 0 or negative, the result is an empty array.

Example: 3

sz1,sz2,...,szN — Tiling factors for each dimension of Atwo or more scalars

Tiling factors for each dimension of A, specified as two or more scalar values. If any tiling factor is 0 or negative, the result is an empty array.

Example: 2,3

Example: 2,3,4,2

sz — Tiling vectorrow vector

Tiling vector, specified as a row vector. If any value in sz is 0 or negative, the result is an empty array.

Example: [2 3]

Example: [2 3 4 2]

More About

expand all

Tips

  • You can use bsxfun to tile arrays and perform a binary operation in a single pass. In some cases, bsxfun provides a simpler and more memory efficient solution. For example, to add the vectors, x=1:5 and y=(1:10)' to produce a 10-by-5 array, use bsxfun(@plus,x,y) instead of repmat(x,10,1) + repmat(y,1,5).

  • You can use other functions to get the same result as repmat when A is a scalar of a certain type.

    repmat SyntaxEquivalent Alternative
    repmat(NaN,m,n)NaN(m,n)
    repmat(single(inf),m,n)inf(m,n,'single')
    repmat(int8(0),m,n)zeros(m,n,'int8')
    repmat(uint32(1),m,n)ones(m,n,'uint32')
    repmat(eps,m,n)eps(ones(m,n))

See Also

| | | | | | |

Was this topic helpful?