Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB reshape: Syntax, Column-Major Order and []

MATLAB reshape syntax, the [] placeholder, column-major order and a derived row-wise pattern, checked against MathWorks and Octave documentation.

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

  1. B = reshape(A,sz) where sz is a vector of dimension sizes, for example [5,2].
  2. B = reshape(A,sz1,...,szN) where each size is a separate argument, for example reshape(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

  1. Create the data: A = 1:10;
  2. Reshape it: B = reshape(A,[5,2])
  3. 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.

  1. Decide the target: 2 rows by 3 columns, filled by rows.
  2. Reshape to the swapped size: reshape(1:6,3,2) has columns [1 2 3] and [4 5 6].
  3. 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) with prod(sz) first; the documentation requires them to be equal. With a [] placeholder, check that the explicit sizes divide numel(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]) or reshape(A,1,[]) for a row vector.

Pre-flight checklist

  1. Run numel(A).
  2. Multiply your intended sizes and confirm the product equals it.
  3. 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).

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB reshape: Syntax, Column-Major Order and []

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →