reshape
Reshape array
Description
reshapesB
= reshape(A
,sz1,...,szN
)A
into asz1
-by-...
-by-szN
array wheresz1,...,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 inB
matches the number of elements inA
. For example, ifA
is a 10-by-10 matrix, thenreshape(A,2,2,[])
reshapes the 100 elements ofA
成2 2-25 array.
Examples
Reshape Vector into Matrix
Reshape a 1-by-10 vector into a 5-by-2 matrix.
A = 1:10; B = reshape(A,[5,2])
B =5×21 6 2 7 3 8 4 9 5 10
Reshape Matrix to Have Specified Number of Columns
Reshape a 4-by-4 square matrix into a matrix that has 2 columns. Specify[]
for the first dimension to letreshape
automatically calculate the appropriate number of rows.
A = magic(4)
A =4×416 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
B = reshape(A,[],2)
B =8×216 3 5 10 9 6 4 15 2 13 11 8 7 12 14 1
The result is an 8-by-2 matrix, which maintains the same number of elements as the original matrix. The elements inB
also maintain their columnwise order fromA
.
Reshape Multidimensional Array into Matrix
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 =9×20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Input Arguments
A
—Input array
vector|matrix|multidimensional array
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
|struct
|datetime
|duration
|calendarDuration
Complex Number Support:Yes
sz
—Output size
row vector of integers
Output size, specified as a row vector of integers. Each element ofsz
indicates the size of the corresponding dimension inB
. You must specifysz
so that the number of elements inA
andB
are the same. That is,prod(sz)
must be the same asnumel(A)
.
Beyond the second dimension, the output,B
, does not reflect trailing dimensions with a size of1
. 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])
sz1,...,szN
—Size of each dimension
two or more integers|[]
(optional)
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 thatnumel(B)
matchesnumel(A)
. When you use[]
to automatically calculate a dimension size, the dimensions that youdoexplicitly 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 of1
. 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
B
— Reshaped array
vector | matrix | multidimensional array | cell array
Reshaped array, returned as a vector, matrix, multidimensional array, or cell array. The data type and number of elements inB
are the same as the data type and number of elements inA
. The elements inB
preserve their columnwise ordering fromA
.
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
|char
|string
|cell
|datetime
|duration
|calendarDuration
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
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, seeTall Arrays for Out-of-Memory Data.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If the input is a compile-time empty cell array, then the size arguments must be constants.
Size arguments must have a fixed size.
SeeVariable-Sizing Restrictions for Code Generation of Toolbox Functions(MATLAB Coder).
For sparse matrices, the
reshape
function does not support trailing ones as inputs after the first two dimensions.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
If the input is a compile-time empty cell array, then the size arguments must be constants.
Size arguments must have a fixed size.
For sparse matrices, the
reshape
function does not support trailing ones as inputs after the first two dimensions.
HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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.
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:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
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
- United Kingdom(English)