repelem

Duplicate copies of array elements

grammar

u = repelem(v,n) B = repelem(A,r1,… ,rN)

instructions

U = repelem(v,n) returns a vector that repeats the elements in v, where v is a scalar or vector.

  • If n is a scalar, then each element of v is repeated n times. The length of u is length of v times n.

  • If n is a vector, it must have the same length as v. Each element of n specifies the number of times the corresponding element of v is repeated.

B = repelem(A,r1,… RN) assign each element in A to r1… RN repeats and returns an array. r1,… Every member of rN has to be A scalar, or A vector of 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×3 block for each element in A.

The sample

Repeating vector element

Create a vector and duplicate each of its elements three times into a new vector.

v = [1 2 3 4];
u = repelem(v,3)
Copy the code

U = 1 x 12

1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4Copy the code

Repeat the first two elements of v twice and the last two three times.

Repelem (v,[2 2 3 3]) u = 1×10

1, 1, 2, 2, 3, 3, 3, 4, 4Copy the code

Repeated matrix element

Create a matrix and copy each of its elements into a 3×2 block of a new matrix. A = [1 2; 3 4] A = 2×2

 1     2
 3     4
Copy the code

B = repelem(A,3,2) B = 6 times 4

1 1 2 2 2 1 1 2 2 3 4 4 3 3 4 4Copy the code

Repeated matrix column

Create a matrix and copy its columns into the new array, repeating the first column twice and the second column three times. A = [1 2; 3 4] A = 2×2

 1     2
 3     4
Copy the code

Repelem (A,1,[2 3]) B = 2×5

1, 1, 2, 2, 3, 3, 4, 4, 4Copy the code

repmat

Duplicate array copy

grammar

B = repmat(A,n)
B = repmat(A,r1,... ,rN) B =repmat(A,r)
Copy the code

instructions

B = repmat(A,n) returns an array containing n copies of A in its row dimension and column dimension. When A is A matrix, B is size(A)n. B = repmat(A,r1,… ,rN) specifies a list of scalars r1,.. RN, these scalars are used to describe how copies of A are arranged in each dimension. When A has N dimensions, B has size(A).[r1…rN]. For example, repmat([1 2; 3 4],2,3) returns a 4×6 matrix. B = repmat(A,r) specifies the repetition scheme using the row vector r. For example, repmat(A,[2 3]) returns the same result as repmat(A,2,3).

The sample

Initialize the matrix with the same element values

Create a 3×2 matrix where all elements are 10. A = repmat(10,3,2) A = 3 times 2

10, 10, 10, 10, 10, 10Copy the code

A square format

Repeat the matrix copy into a 2×2 block arrangement. A = diag([100 200 300]) A = 3×3

100 00 0 200 00 0 300Copy the code

B = repmat(A,2) B = 6 times 6

100 00 100 00 0 200 00 200 00 300 00 300 100 00 100 00 0 200 00 200 00 0 300 00 300Copy the code

Rectangular block format

Repeat the matrix copy into a 2×3 block arrangement. A = diag([100 200 300]) A = 3×3

100 00 0 200 00 0 300Copy the code

B = repmat(A,2,3)

B = 6×9

100 0 0 0 0 0 0 0 0 0 200 100 100 200 200 0 0 0 0 0 0 0 0 0 0 0 300 100 300 300 100 100 0 0 0 0 0 0 0 0 0 0 200 200 200 00 300 00 300 00 300Copy the code

kron

The Kronecker tensor product

grammar

K = kron(A,B)

instructions

K = kron(A,B) returns the Kronecker tensor product of matrices A and B. If A is an M by n matrix and B is A P by Q matrix, then kron(A,B) is an M *p by n*q matrix formed by taking all possible products between the elements of A and the elements of the matrix B.

The sample

Repeated matrix element

Expand the matrix size by repeating elements. Create 2×2 matrices and 2×3 matrices that need to repeat their elements.

A = [1 2 3; 4 5 6];
B = ones(2);
Copy the code

Kron is used to compute the Kronecker tensor product.

K = kron(A,B)
K = 4x6
Copy the code
1. 1 2 2 3 3 4 5 5 6 6 6 6Copy the code

The result is a 4×6 block matrix.

conclusion

  • Repelem and kron can be used to repeat array elements, whose units are the elements in the array.
  • Repmat repeats arrays in units of the array as a whole.

References: [1] ww2. Mathworks. Cn/help/matlab… [2] ww2. Mathworks. Cn/help/matlab… [3] ww2. Mathworks. Cn/help/matlab…