How to Reshape a Matrix in MATLAB

The function reshape reshapes a matrix.


Create a matrix using the following code:

A = [1:6;7:12];

Syntax (I)

B = reshape(A,sz) copies the elements in matrix A to a new matrix B of size sz. sz is normally a row vector and must contain at least two elements.

Both matrices A and B are processed in their linear form, so B(i) = A(i).
% A as a 3-by-4 matrix
resultExample1 = reshape(A,[3,4]);
% A as a 3-by-2-by-2 matrix
resultExample2 = reshape(A,[3,2,2]);

Syntax (II)

B = reshape(A,sz1,…,szN) copies the elements in matrix A to a new matrix B of size determined by the comma-separated list sz1,…,szN.

If a value in the list is [], MATLAB calculates the size of the corresponding dimension automatically. The list must have at least two elements and only one can be [].

% A as a 6-by-2 matrix
resultExample3 = reshape(A,6,2);
% A as a 4-by-3 matrix
resultExample4 = reshape(A,[],3);

Note: In both syntaxes, the output size must guarantee that the number of elements in A and B is equal.

Further reading

I recommend the following books to learn more on matrix reshaping in MATLAB.

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a 2017 Textbook Excellence Award. The second chapter explains how to perform a wide list of operations on matrices, including matrix reshaping.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Matrix manipulation is explained in the fourth chapter.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series.

doc reshape;


Create a row vector using the following code:

X = 1:16;

Reshape X so it has:

  1. Two rows and eight columns
  2. Eight rows and two columns
  3. Four rows and four columns

Source code

I hope you have learned how to reshape a matrix in MATLAB. The source code developed in this tutorial is available at this page.


Your friends may need this knowledge to complete a task. Share this tutorial.