Written and maintained by CASRAI Editorial Board
Last updated
reshape in MATLAB changes the dimensions of an array without changing its elements: B = reshape(A,sz) returns the same data as A arranged in the size sz. This guide is for researchers and analysts who need to turn a vector into a matrix, flatten an image stack, or fill a matrix row by row, and who want to know why the output order is what it is. Three things decide every reshape: the element count must match, MATLAB fills columns first, and one dimension may be left as [].
What it is used for in research
Measurement data rarely arrives in the shape an analysis needs. A logged signal arrives as one long vector but must be analysed as trials-by-samples; a stack of images stored as height-by-width-by-slices may need to become a two-dimensional matrix with one column per slice so that a matrix function can run on it; a column of results may need to become a grid for plotting. reshape does these conversions without altering values or the data type. Per the MathWorks documentation, it only rearranges elements: to add or remove elements you use a different function (resize), not reshape.
Install or access (official source only)
reshape is a built-in MATLAB function and needs no toolbox. MATLAB is obtained from MathWorks at mathworks.com, and its function reference is at mathworks.com/help/matlab. GNU Octave, a free alternative that supports the same function, is obtained from the GNU Octave project at octave.org. Licensing terms for either product are set by their publishers and are not covered here.
Step-by-step: reshape a vector or matrix
Syntax
B = reshape(A,sz)whereszis a vector of dimension sizes, for example[5,2].B = reshape(A,sz1,...,szN)where each size is a separate argument, for examplereshape(A,5,2).
The size vector must contain at least two elements, and prod(sz) must equal numel(A) (MathWorks documentation, read 2026-11-07).
Worked example: a vector to a matrix
- Create the data:
A = 1:10; - Reshape it:
B = reshape(A,[5,2]) - Read the result: the first column is 1 2 3 4 5 and the second is 6 7 8 9 10. The values are taken in their original order and poured into the first column, then the second.
Let MATLAB compute one dimension with []
You may replace one dimension with [] and MATLAB calculates it from numel(A). The sizes you do give must divide evenly into the element count, and only one placeholder is allowed. The documentation’s examples:
A = magic(4); B = reshape(A,[],2)gives an 8-by-2 matrix.- For a 10-by-10 matrix
A,reshape(A,2,2,[])gives a 2-by-2-by-25 array.
Output and how to read it
Column-major order: the rule behind every surprise
Elements keep their columnwise ordering from A. MATLAB reads A down the first column, then the second, and so on, then writes into B in the same way. Reshaping is therefore a relabelling of one fixed sequence, and you can predict any result by writing the elements out column by column.
A multi-dimensional case from the documentation: rng default; A = rand(2,2,3); B = reshape(A,6,2) produces a 6-by-2 matrix whose first column begins 0.8147, 0.9058, 0.1270, 0.9134, 0.6324, 0.0975.
Reshape row-wise (C-order)
Because filling is column-first, reshape(1:6,2,3) returns the rows [1 3 5] and [2 4 6], not [1 2 3] and [4 5 6]. To fill row by row, derive the answer from the same rule: reshape to the transposed size, then transpose.
- Decide the target: 2 rows by 3 columns, filled by rows.
- Reshape to the swapped size:
reshape(1:6,3,2)has columns[1 2 3]and[4 5 6]. - Transpose:
reshape(1:6,3,2)'has rows[1 2 3]and[4 5 6].
This is a derivation from the column-major rule above, not a separate documented feature. For higher-dimensional arrays, row-wise ordering generally requires reordering dimensions with permute as well. See the transpose in MATLAB guide for the transpose operators.
Trailing singleton dimensions are dropped
reshape(A,[3,2,1,1]) returns a 3-by-2 array, because trailing dimensions of size 1 beyond the second are removed.
Reshape, resize and transpose compared
| Goal | Tool | Changes the element count? |
|---|---|---|
| Same elements, new dimensions | reshape |
No: prod(sz) must equal numel(A) |
| Add or remove elements | resize (named in the reshape documentation) | Yes |
| Swap rows and columns of a 2-D array | transpose (A' or A.') |
No |
Troubleshooting
- The call errors out. Compare
numel(A)withprod(sz)first; the documentation requires them to be equal. With a[]placeholder, check that the explicit sizes dividenumel(A)without remainder. - Two placeholders. Only one dimension can be
[]. - Rows come out in the wrong order. You expected row-wise filling; use the swapped-size-then-transpose pattern above.
- A size vector with one element. It must have at least two elements; use
reshape(A,[1,n])orreshape(A,1,[])for a row vector.
Pre-flight checklist
- Run
numel(A). - Multiply your intended sizes and confirm the product equals it.
- Write the first column of the result by hand from the column-major rule and compare it with
B(:,1).
Related reading: the matrix in MATLAB guide covers matrix basics, the colon operator in MATLAB covers A(:) style indexing, and MATLAB cell arrays covers containers for mixed-size data.
Free alternatives (table)
| Tool | Same call? | Placeholder behaviour (per its documentation) |
|---|---|---|
| MATLAB (MathWorks) | reshape(A,sz) |
One dimension may be []; computed automatically |
| GNU Octave (free) | reshape(A,...) |
“A single dimension of the return matrix may be left unspecified”; element totals must match |
FAQ
What does reshape do in MATLAB?
It returns an array with the same elements as the input in new dimensions, keeping their column-wise order and data type.
How do I reshape a matrix row-wise in MATLAB?
Reshape to the swapped dimensions and transpose, for example reshape(A,cols,rows)' for a vector A.
Can reshape change the number of elements?
No. The product of the new sizes must equal numel(A).
What does [] mean in reshape(A,[],2)?
It tells MATLAB to compute that dimension. Only one is allowed, and the other sizes must divide the element count evenly.
Sources (official docs)
- MathWorks, MATLAB documentation, reshape: mathworks.com/help/matlab/ref/double.reshape.html (read 2026-11-07).
- GNU Octave documentation, Rearranging Matrices: docs.octave.org/latest/Rearranging-Matrices.html (read 2026-11-07).







