repelem - Repeat copies of array elements - MATLAB (original) (raw)
Repeat copies of array elements
Syntax
Description
[u](#bum31rc-1-u) = repelem([v](#bum31rc-1-v),[n](#bum31rc-1-n))
, where v
is a scalar or vector, returns a vector of repeated elements of v
.
- If
n
is a scalar, then each element ofv
is repeatedn
times. The length ofu
islength(v)*n
. - If
n
is a vector, then it must be the same length asv
. Each element ofn
specifies the number of times to repeat the corresponding element ofv
.
This syntax is not supported for table
input.
[B](#bum31rc-1-B) = repelem([A](#bum31rc-1-A),[r1,...,rN](#bum31rc-1-r1rN))
returns an array with each element of A
repeated according to r1,...,rN
. Each r1,...,rN
must either be a scalar or a vector with the same length as A
in the corresponding dimension. For example, if A
is a matrix, repelem(A,2,3)
returns a matrix containing a 2
-by-3
block of each element of A
.
Examples
Create a vector and repeat each of its elements three times into a new vector.
v = [1 2 3 4]; u = repelem(v,3)
u = 1×12
1 1 1 2 2 2 3 3 3 4 4 4
Repeat the first two elements of v
twice and the last two elements three times.
u = 1×10
1 1 2 2 3 3 3 4 4 4
Create a matrix and repeat each element into a 3-by-2 block of a new matrix.
B = 6×4
1 1 2 2
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
3 3 4 4
Create a matrix and copy its columns into a new array, repeating the first column twice and second column three times.
B = 2×5
1 1 2 2 2
3 3 4 4 4
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 element, specified as a scalar or a vector.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| datetime
| duration
Complex Number Support: Yes
Number of times to repeat each element, specified as a scalar or a vector. If n
is a scalar, then all elements of v
are repeated n
times. If n
is a vector, then each element of n
specifies the number of times to repeat the corresponding element of v
. In either case, n
must be integer-valued.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Input array, specified as a matrix or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| struct
| table
| cell
| datetime
| duration
Complex Number Support: Yes
Repetition factors for each dimension, specified as separate arguments of integer-valued scalars or vectors. If A
is a table, each repetition factor must be a scalar.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
Output vector. If v
is a row vector or scalar, u
is a row vector. If v
is a column vector, u
is also a column vector.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| datetime
| duration
Output array, returned as a matrix or multidimensional array.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| datetime
| duration
Extended Capabilities
This function supports tall arrays with the limitations:
- The two-input syntax is not supported.
- The replication factor in the first dimension must be 1. For example,
repelem(TA,1,n,p,...)
.
For more information, see Tall Arrays.
Usage notes and limitations:
- The input must be a vector or matrix. The input cannot be a multidimensional array.
Usage notes and limitations:
- The input must be a vector or matrix. The input cannot be a multidimensional array.
The repelem
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 in R2015a