Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

reshape

Syntax

Description

example

B = reshape(A,sz) reshapes A using the size vector, sz, to define size(B). For example, reshape(A,[2,3]) reshapes A into a 2-by-3 matrix. sz must contain at least 2 elements, and prod(sz) must be the same as numel(A).

example

B = reshape(A,sz1,...,szN) reshapes A into a sz1-by-...-by-szN array where sz1,...,szN indicates the size of each dimension. You can specify a single dimension size of [] to have the dimension size automatically calculated, such that the number of elements in B matches the number of elements in A. For example, if A is a 10-by-10 matrix, then reshape(A,2,2,[]) reshapes the 100 elements of A into a 2-by-2-by-25 array.

Examples

collapse all

Reshape a 1-by-10 vector into a 5-by-2 matrix.

A = 1:10;
B = reshape(A,[5,2])
B =

     1     6
     2     7
     3     8
     4     9
     5    10

Reshape a 6-by-6 magic square matrix into a matrix that has only 3 columns. Specify [] for the first dimension size to let reshape automatically calculate the appropriate number of rows.

A = magic(6);
B = reshape(A,[],3)
B =

    35     6    19
     3     7    23
    31     2    27
     8    33    10
    30    34    14
     4    29    18
     1    26    24
    32    21    25
     9    22    20
    28    17    15
     5    12    16
    36    13    11

The result is a 12-by-3 matrix, which maintains the same number of elements (36) as the original 6-by-6 matrix. The elements in B also maintain their columnwise order from A.

Reshape a 3-by-2-by-3 array of zeros into a 9-by-2 matrix.

A = zeros(3,2,3);
B = reshape(A,9,2)
B =

     0     0
     0     0
     0     0
     0     0
     0     0
     0     0
     0     0
     0     0
     0     0

Input Arguments

collapse all

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | cell | datetime | duration | calendarDuration
Complex Number Support: Yes

Output size, specified as a row vector of integers. Each element of sz indicates the size of the corresponding dimension in B. You must specify sz so that the number of elements in A and B are the same. That is, prod(sz) must be the same as numel(A).

Beyond the second dimension, the output, B, does not reflect trailing dimensions with a size of 1. For example, reshape(A,[3,2,1,1]) produces a 3-by-2 matrix.

Example: reshape(A,[3,2])

Example: reshape(A,[6,4,10])

Example: reshape(A,[5,5,5,5])

Size of each dimension, specified as two or more integers with at most one [] (optional). You must specify at least 2 dimension sizes, and at most one dimension size can be specified as [], which automatically calculates the size of that dimension to ensure that numel(B) matches numel(A). When you use [] to automatically calculate a dimension size, the dimensions that you do explicitly specify must divide evenly into the number of elements in the input matrix, numel(A).

Beyond the second dimension, the output, B, does not reflect trailing dimensions with a size of 1. For example, reshape(A,3,2,1,1) produces a 3-by-2 matrix.

Example: reshape(A,3,2)

Example: reshape(A,6,[],10)

Example: reshape(A,2,5,3,[])

Example: reshape(A,5,5,5,5)

Output Arguments

collapse all

Reshaped array, returned as a vector, matrix, multidimensional array, or cell array. The data type and number of elements in B are the same as the data type and number of elements in A. The elements in B preserve their columnwise ordering from A.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | cell | datetime | duration | calendarDuration

More About

collapse all

Tall Array Support

This function supports tall arrays with the limitations:

Reshaping the tall dimension (dimension one) is not supported. The first dimension input should always be empty, such as reshape(X,[],M,N,...).

For more information, see Tall Arrays.

See Also

| | | |

Introduced before R2006a

Was this topic helpful?