Repeat copies of array - MATLAB repmat (2024)

Repeat copies of array

collapse all in page

Syntax

B = repmat(A,n)

B = repmat(A,r1,...,rN)

B = repmat(A,r)

Description

example

B = repmat(A,n) returnsan array containing n copies of A inthe row and column dimensions. The size of B is size(A)*n when A isa matrix.

example

B = repmat(A,r1,...,rN) specifiesa list of scalars, r1,..,rN, that describes howcopies 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-6matrix.

example

B = repmat(A,r) specifiesthe repetition scheme with row vector r. For example, repmat(A,[23]) returns the same result as repmat(A,2,3).

Examples

collapse all

Initialize Matrix with Same Element Value

Open Live Script

Create a 3-by-2 matrix whose elements contain the value 10.

A = repmat(10,3,2)
A = 3×2 10 10 10 10 10 10

Square Block Format

Open Live Script

Repeat copies of a matrix into a 2-by-2 block arrangement.

A = diag([100 200 300])
A = 3×3 100 0 0 0 200 0 0 0 300
B = repmat(A,2)
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

Rectangular Block Format

Repeat copies of a matrix into a 2-by-3 block arrangement.

A = diag([100 200 300])
A = 3×3 100 0 0 0 200 0 0 0 300
B = repmat(A,2,3)
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

3-D Block Array

Open Live Script

Repeat copies of a matrix into a 2-by-3-by-2 block arrangement.

A = [1 2; 3 4]
A = 2×2 1 2 3 4
B = repmat(A,[2 3 2])
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 4B(:,:,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

Vertical Stack of Row Vectors

Open Live Script

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

Horizontal Stack of Column Vectors

Open Live Script

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

Tabular Block Format

Open Live Script

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 = repmat(A,2,3)
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.

Combine Vector Elements

Open Live Script

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 = combinations(A,B)
T=6×2 table A B _ _ 1 2 1 4 3 2 3 4 5 2 5 4

Input Arguments

collapse all

AInput array
scalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensionalarray.

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

nNumber of times to repeat input array in row and column dimensions
integer value

Number of times to repeat the input array in the row and columndimensions, specified as an integer value. If n is 0 ornegative, the result is an empty array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

r1,...,rNRepetition factors for each dimension (as separate arguments)
integer values

Repetition factors for each dimension, specified as separatearguments of integer values. If any repetition factor is 0 ornegative, the result is an empty array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

rVector of repetition factors for each dimension (as a row vector)
integer values

Vector of repetition factors for each dimension, specified asa row vector of integer values. If any value in r is 0 ornegative, 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 productof the input with an array of ones, use kron.For example, to stack the row vector A = 1:3 fourtimes vertically, you can use B = kron(A,ones(4,1)).

  • To create block arrays and perform a binary operationin a single pass, use bsxfun.In some cases, bsxfun provides a simpler andmore memory efficient solution. For example, to add the vectors A= 1:5 and B = (1:10)' to produce a 10-by-5array, use bsxfun(@plus,A,B) instead of repmat(A,10,1)+ repmat(B,1,5).

  • When A is a scalar of a certaintype, you can use other functions to get the same result as repmat.

    repmat SyntaxEquivalentAlternative
    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

HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

See Also

bsxfun | kron | repelem | reshape | resize | paddata | meshgrid | ndgrid

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Repeat copies of array - MATLAB repmat (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Repeat copies of array - MATLAB repmat (2024)

FAQs

How to repeat numbers in an array in MATLAB? ›

u = repelem( v , n ) , where v is a scalar or vector, returns a vector of repeated elements of v . If n is a scalar, then each element of v is repeated n times. The length of u is length(v)*n .

What does the repmat function do in MATLAB? ›

The MATLAB® command repmat is used to replicate and tile arrays. It works on the built-in objects of MATLAB, namely double , char , as well as the generalized container objects cell and struct . The identical functionality is provided for replicating and tiling uncertain elements ( ureal , ultidyn , etc.)

What is the most repeated value in MATLAB? ›

M = mode( A ) returns the sample mode of A , which is the most frequently occurring value in A . When there are multiple values occurring equally frequently, mode returns the smallest of those values.

How to repeat a vector multiple times in MATLAB? ›

B = repmat( A , r ) specifies the repetition scheme with row vector r . For example, repmat(A,[2 3]) returns the same result as repmat(A,2,3) .

How do you repeat a value in an array? ›

repeat() function is used to repeat elements of an array. It takes an array and a repetition count as inputs and outputs a new array with the original array elements repeated based on the repetition count.

How do you check how many times a value is repeated in array? ›

The task of counting duplicates in a JavaScript array can be accomplished by using the forEach method. This method allows us to iterate through the array, incrementing the count of each item and storing the results in an object.

How do you copy an array in MATLAB? ›

B = copy( A ) copies each element in the array of handles A to a new array of handles B . The copy method follows these rules: The copy method does not copy dependent properties. MATLAB® does not call the copy method recursively on any handles contained in property values.

What is the repeat function in MATLAB? ›

Description. repeat( action , n ) repeats the same action n times. You can specify the input arguments in any order. That is, repeat(action,n) and repeat(n,action) both repeat the action n times.

How to multiply arrays in MATLAB? ›

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

How do you find most repeated numbers in an array? ›

To find the most common element in an array, iterate through the array and keep track of the frequency of each element using a hash table or dictionary. Then, find the element with the highest frequency.

What is repeating sequence in MATLAB? ›

Description. The Repeating Sequence Interpolated block outputs a periodic discrete-time sequence based on the values in Vector of time values and Vector of output values parameters. Between data points, the block uses the method you specify for the Lookup Method parameter to determine the output.

Which value is most repeated? ›

The mode is the value that appears most frequently in a data set.

What does squeeze do in MATLAB? ›

Description. B = squeeze(A) returns an array B with the same elements as A but with all the singleton dimensions removed. A singleton is a dimension such that size(A,dim)==1 .

How to use mat2cell? ›

mat2cell (MATLAB Functions) c = mat2cell(x,m,n) divides up the two-dimensional matrix x into adjacent submatrices, each contained in a cell of the returned cell array, c . Vectors m and n specify the number of rows and columns, respectively, to be assigned to the submatrices in c .

How do you repeat a vector? ›

Repeat a vector
  1. vec_rep() repeats an entire vector a set number of times .
  2. vec_rep_each() repeats each element of a vector a set number of times .
  3. vec_unrep() compresses a vector with repeated values. The repeated values are returned as a key alongside the number of times each key is repeated.

How do you multiply numbers in an array in MATLAB? ›

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

How do you print repeated numbers in an array? ›

Algorithm
  1. Declare and initialize an array.
  2. Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. ...
  3. If a match is found which means the duplicate element is found then, display the element.

How do you repeat something in MATLAB? ›

repeat( action , n ) repeats the same action n times. You can specify the input arguments in any order. That is, repeat(action,n) and repeat(n,action) both repeat the action n times.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Greg O'Connell

Last Updated:

Views: 6480

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.