Skip to Main Content Skip to Search
Product Documentation

repmat - Replicate and tile array

Syntax

B = repmat(A,m,n)
B = repmat(A,[m n])
B = repmat(A,[m n p...])

Description

B = repmat(A,m,n) creates a large matrix B consisting of an m-by-n tiling of copies of A. The size of B is [size(A,1)*m, (size(A,2)*n]. The statement repmat(A,n) creates an n-by-n tiling.

B = repmat(A,[m n]) accomplishes the same result as repmat(A,m,n).

B = repmat(A,[m n p...]) produces a multidimensional array B composed of copies of A. The size of B is [size(A,1)*m, size(A,2)*n, size(A,3)*p, ...].

Tips

repmat(A,m,n), when A is a scalar, produces an m-by-n matrix filled with A's value and having A's class. For certain values, you can achieve the same results using other functions, as shown by the following examples:

Examples

Example 1

In this example, repmat replicates 12 copies of the second-order identity matrix, resulting in a "checkerboard" pattern.

B = repmat(eye(2),3,4)

B =
     1     0     1     0     1     0     1     0
     0     1     0     1     0     1     0     1
     1     0     1     0     1     0     1     0
     0     1     0     1     0     1     0     1
     1     0     1     0     1     0     1     0
     0     1     0     1     0     1     0     1 

The statement N = repmat(NaN,[2 3]) creates a 2-by-3 matrix of NaNs.

Example 2

If you have code that uses repmat and also a binary operator or function, you can transform the code to use the bsxfun function instead. In certain cases, this can provide a simpler and faster solution.

This example replaces the sum of two repmat operations with a single call to bsxfun:

x = 1:5; y = (1:10)'; 

% Replace this code
repmat(x,10,1) + repmat(y,1,5) 

% with the following:
bsxfun(@plus, x, y)

See Also

bsxfun | Inf | NaN | ones | reshape | zeros

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS