Main Content

reshape

Reshape symbolic array

Description

example

reshape(A,n1,n2) returns the n1-by-n2 matrix, which has the same elements as A. The elements are taken column-wise from A to fill in the elements of the n1-by-n2 matrix.

example

reshape(A,n1,...,nM) returns the n1-by-...-by-nM array, which has the same elements as A. The elements are taken column-wise from A to fill in the elements of the n1-by-...-by-nM array.

example

reshape(A,...,[],...) lets you represent a size value with the placeholder [] while calculating the magnitude of that size value automatically. For example, if A has size 2-by-6, then reshape(A,4,[]) returns a 4-by-3 array.

example

reshape(A,sz) reshapes A into an array with size specified by sz, where sz is a vector.

Examples

Reshape Symbolic Row Vector into Column Vector

Reshape V, which is a 1-by-4 row vector, into the 4-by-1 column vector Y. Here, V and Y must have the same number of elements.

Create the vector V.

syms f(x) y
V = [3 f(x) -4 y]
V =
[ 3, f(x), -4, y]

Reshape V into Y.

Y = reshape(V,4,1)
Y =
   3
f(x)
  -4
   y

Alternatively, use Y = V.' where .' is the nonconjugate transpose.

Reshape Symbolic Matrix

Reshape the 2-by-6 symbolic matrix M into a 4-by-3 matrix.

M = sym([1 9 4 3 0 1; 3 9 5 1 9 2])
N = reshape(M,4,3)
M =
[ 1, 9, 4, 3, 0, 1]
[ 3, 9, 5, 1, 9, 2]
 
N =
[ 1, 4, 0]
[ 3, 5, 9]
[ 9, 3, 1]
[ 9, 1, 2]

M and N must have the same number of elements. reshape reads M column-wise to fill in the elements of N column-wise.

Alternatively, use a size vector to specify the dimensions of the reshaped matrix.

sz = [4 3];
N = reshape(M,sz)
N =
[ 1, 4, 0]
[ 3, 5, 9]
[ 9, 3, 1]
[ 9, 1, 2]

Automatically Set Dimension of Reshaped Matrix

When you replace a dimension with the placeholder [], reshape calculates the required magnitude of that dimension to reshape the matrix.

Create the matrix M.

M = sym([1 9 4 3 0 1; 3 9 5 1 9 2])
M =
[ 1, 9, 4, 3, 0, 1]
[ 3, 9, 5, 1, 9, 2]

Reshape M into a matrix with three columns.

reshape(M,[],3)
ans =
[ 1, 4, 0]
[ 3, 5, 9]
[ 9, 3, 1]
[ 9, 1, 2]

reshape calculates that a reshaped matrix of three columns needs four rows.

Reshape Matrix Row-wise

Reshape a matrix row-wise by transposing the result.

Create matrix M.

syms x
M = sym([1 9 0 sin(x) 2 2; NaN x 5 1 4 7])
M =
[   1, 9, 0, sin(x), 2, 2]
[ NaN, x, 5,      1, 4, 7]

Reshape M row-wise by transposing the result.

reshape(M,4,3).'
ans =
[ 1, NaN,      9, x]
[ 0,   5, sin(x), 1]
[ 2,   4,      2, 7]

Note that .' returns the non-conjugate transpose while ' returns the conjugate transpose.

Reshape 3-D Array into 2-D Matrix

Reshape the 3-by-3-by-2 array M into a 9-by-2 matrix.

M has 18 elements. Because a 9-by-2 matrix also has 18 elements, M can be reshaped into it. Construct M.

syms x
M = [sin(x) x 4; 3 2 9; 8 x x];
M(:,:,2) = M'
M(:,:,1) = 
[ sin(x), x, 4]
[      3, 2, 9]
[      8, x, x]
M(:,:,2) = 
[ sin(conj(x)), 3,       8]
[      conj(x), 2, conj(x)]
[            4, 9, conj(x)]

Reshape M into a 9-by-2 matrix.

N = reshape(M,9,2)
N =
[ sin(x), sin(conj(x))]
[      3,      conj(x)]
[      8,            4]
[      x,            3]
[      2,            2]
[      x,            9]
[      4,            8]
[      9,      conj(x)]
[      x,      conj(x)]

Use reshape to Break Up Arrays

Use reshape instead of loops to break up arrays for further computation. Use reshape to break up the vector V to find the product of every three elements.

Create vector V.

syms x
V = [exp(x) 1 3 9 x 2 7 7 1 8 x^2 3 4 sin(x) x]
V =
[ exp(x), 1, 3, 9, x, 2, 7, 7, 1, 8, x^2, 3, 4, sin(x), x]

Specify 3 for the number of rows. Use the placeholder [] for the number of columns. This lets reshape automatically calculate the number of columns required for three rows.

M = prod( reshape(V,3,[]) )
M =
[ 3*exp(x), 18*x, 49, 24*x^2, 4*x*sin(x)]

reshape calculates that five columns are required for a matrix of three rows. prod then multiples the elements of each column to return the result.

Input Arguments

collapse all

Input array, specified as a symbolic vector, matrix, or multidimensional array.

Dimensions of reshaped matrix, specified as comma-separated scalars. For example, reshape(A,3,2) returns a 3-by-2 matrix. The number of elements in the output array specified by n1,n2 must be equal to numel(A).

Dimensions of reshaped array, specified as comma-separated scalars. For example, reshape(A,3,2,2) returns a 3-by-2-by-2 matrix. The number of elements in the output array specified by n1,...,nM must be equal to numel(A).

Size of reshaped array, specified as a numeric vector. For example, reshape(A,[3 2]) returns a 3-by-2 matrix. The number of elements in the output array specified by sz must be equal to numel(A).

Version History

Introduced before R2006a

See Also

|