repmat - Repeat copies of array - MATLAB (original) (raw)
Syntax
Description
B = repmat([A](#btzavfc-1-A),[n](#btzavfc-1-n))
returns an array containing n
copies of A
in the row and column dimensions. The size of B
is size(A)*n
when A
is a matrix.
B = repmat([A](#btzavfc-1-A),[r1,...,rN](#btzavfc-1-r1rN))
specifies a list of scalars, r1,..,rN
, that describes how copies of A
are arranged in each dimension. When A
has N
dimensions, the size of B
is size(A).*[r1...rN]
. For example, repmat([1 2; 3 4],2,3)
returns a 4-by-6 matrix.
B = repmat([A](#btzavfc-1-A),[r](#btzavfc-1-r))
specifies the repetition scheme with row vector r
. For example, repmat(A,[2 3])
returns the same result as repmat(A,2,3)
.
Examples
Create a 3-by-2 matrix whose elements contain the value 10.
A = 3×2
10 10
10 10
10 10
Repeat copies of a matrix into a 2-by-2 block arrangement.
A = 3×3
100 0 0 0 200 0 0 0 300
B = 6×6
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
Repeat copies of a matrix into a 2-by-3 block arrangement.
A = 3×3
100 0 0 0 200 0 0 0 300
B = 6×9
100 0 0 100 0 0 100 0 0 0 200 0 0 200 0 0 200 0 0 0 300 0 0 300 0 0 300 100 0 0 100 0 0 100 0 0 0 200 0 0 200 0 0 200 0 0 0 300 0 0 300 0 0 300
Repeat copies of a matrix into a 2-by-3-by-2 block arrangement.
B = B(:,:,1) =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
B(:,:,2) =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
Vertically stack a row vector four times.
A = 1:4; B = repmat(A,4,1)
B = 4×4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Horizontally stack a column vector four times.
A = (1:3)';
B = repmat(A,1,4)
B = 3×4
1 1 1 1
2 2 2 2
3 3 3 3
Create a table with variables Age
and Height
.
A = table([39; 26],[70; 63],'VariableNames',{'Age' 'Height'})
A=2×2 table Age Height ___ ______
39 70
26 63
Repeat copies of the table into a 2-by-3 block format.
B=4×6 table Age Height Age_1 Height_1 Age_2 Height_2 ___ ______ _____ ________ _____ ________
39 70 39 70 39 70
26 63 26 63 26 63
39 70 39 70 39 70
26 63 26 63 26 63
repmat
repeats the entries of the table and appends a number to the new variable names.
Create two column vectors.
A = [1; 3; 5]; B = [2; 4];
Generate all element combinations of the two vectors by using repelem
and repmat
. Each row of the output T
is a combination with the first element coming from the first vector and the second element coming from the second vector. This command is equivalent to finding the Cartesian product of two vectors.
T = [repelem(A,numel(B)) repmat(B,numel(A),1)]
T = 6×2
1 2
1 4
3 2
3 4
5 2
5 4
Starting in R2023a, you can also use the combinations function to generate all element combinations of two vectors.
T=6×2 table A B _ _
1 2
1 4
3 2
3 4
5 2
5 4
Input Arguments
Input array, specified as a scalar, vector, matrix, or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| datetime
| duration
| calendarDuration
| categorical
| cell
Complex Number Support: Yes
Number of times to repeat the input array in the row and column dimensions, specified as an integer value. If n
is 0
or negative, the result is an empty array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Repetition factors for each dimension, specified as separate arguments of integer values. If any repetition factor is 0
or negative, the result is an empty array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Vector of repetition factors for each dimension, specified as a row vector of integer values. If any value in r
is 0
or negative, the result is an empty array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Tips
- To build block arrays by forming the tensor product of the input with an array of ones, use kron. For example, to stack the row vector
A = 1:3
four times vertically, you can useB = kron(A,ones(4,1))
. - To create block arrays and perform a binary operation in a single pass, use bsxfun. In some cases,
bsxfun
provides a simpler and more memory efficient solution. For example, to add the vectorsA = 1:5
andB = (1:10)'
to produce a 10-by-5 array, usebsxfun(@plus,A,B)
instead ofrepmat(A,10,1) + repmat(B,1,5)
. - When
A
is a scalar of a certain type, you can use other functions to get the same result asrepmat
.repmat Syntax Equivalent 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))
Extended Capabilities
This function supports tall arrays with the limitations:
- The replication factor in the first dimension must be 1. For example,
repmat(TA,1,n,p,...)
.
For more information, see Tall Arrays.
Usage notes and limitations:
- Size arguments must have a fixed size.
- For sparse matrices, the
repmat
function does not support trailing ones as inputs after the first two dimensions.
Usage notes and limitations:
- Size arguments must have a fixed size.
- For sparse matrices, the
repmat
function does not support trailing ones as inputs after the first two dimensions.
The repmat
function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Starting in R2019b, some syntaxes involving nonscalar or empty repetition arguments will produce an error. The following table describes how to update these syntaxes.
Errors | Use Instead |
---|---|
repmat(A,r1,r2), wherer1 and r2 are row vectors | repmat(A,[r1 r2]) |
repmat(A,empt), whereempt is an empty array | repmat(A,1) |
repmat(A,empt1,empt2), whereempt1 and empt2 are empty arrays | repmat(A,1) |
repmat(A,n,empt), wheren is an integer andempt is an empty array | repmat(A,[n 1]) |