Matlab

How to Reshape a Matrix or Vector in MATLAB

MATLAB is a powerful tool that helps you work with matrices efficiently. In some cases, you may need to change the shape of our data, such as turning a vector into a matrix or a multidimensional array. The reshape() is a built-in MATLAB function that is designed specifically for this purpose.

In this article, we will explore the working of reshape() function with some easy examples in MATLAB, allowing users to reshape matrices or vectors in MATLAB.

How to Reshape a Vector or a Matrix in MATLAB?

The reshape() in MATLAB allows users to change the dimension of the array and convert it into another array. For example, it can convert a vector into a matrix and a matrix into a multidirectional array and vice versa. By utilizing the reshape() function, MATLAB users have the flexibility to transform their data structures according to their specific requirements.

The Syntax for reshape() Function in MATLAB

The reshape() function in MATLAB follows a simple syntax that is given below.

B = reshape(A,sz)
B = reshape(A,sz1,...,szN)

 
Here:

B = reshape(A,sz) yields to reshape a given vector, matrix, or, multidirectional array into a specified size sz. Remember that the cardinality of A must be equal to the size sz. For example, if the defined size of A is 1-by-10 then the sz should be 2-by-5 or 5-by 2, as both options have the cardinality of 10. This ensures that the elements of the reshaped array can be rearranged and fit into the specified size without loss or duplication of data.

B = reshape(A,sz1,…,szN) transforms A into an array with dimensions of sz1 by… by szN, where sz1,…,szN denotes the sizes of the various dimensions. If you want the dimension size to be determined automatically so that the number of elements in B and A is the same, you can give a single dimension size of []. For example, if A is a 5-by-10 matrix, reshape(A,2,5,[]) transforms 50 elements of A into a 2-by-5-by-5 array.

How to Use reshape() Function in MATLAB

For more understanding, consider some examples that demonstrate the functionality of the MATLAB reshape() function.

Example 1

The given example defines a vector A of 1-by-10 dimension and transforms it into a matrix B with the size 2-by-5 using the MATLAB reshape() function.

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

 

Example 2

In this MATLAB code, we define a matrix A of 4-by-5 dimension having all 1s and transform it into a matrix B with the size 5-by-4 using the MATLAB reshape() function.

A = ones(4,5);
B = reshape(A,[5,4])

 

Example 3

In this example, we define a multidirectional array of 4-by-5-by-2 dimensions using the rand() function and transform it into a matrix B with the size 8-by-5 using the MATLAB reshape() function.

A = rand(4,5,2);
B =reshape(A,[8,5])

 

Example 4

This example is the reverse process of Example 3. In this example, we define a matrix A of 8-by-5 dimension using the rand() function and transform it into a multidirectional array B with the size 4-by-5-by-2 using the MATLAB reshape() function.

A = rand(8,5);
B =reshape(A,[4,5,2])

 

Note: In the above case, since the reshape operation is applied to a matrix with a total of 40 elements (8 x 5), the resulting B will have dimensions 4-by-5-by-2. Each 2-dimensional submatrix within B will have dimensions 4-by-5, and there will be 2 such submatrices.

Conclusion

The reshape() is a powerful function in MATLAB used for reshaping a vector or a matrix. This function allows us to change the dimension of the array and convert it into another array. For example, it can convert a vector into a matrix and a matrix into a multidirectional array and vice versa. This tutorial explained the use of reshape() function and how to reshape a vector, a matrix, or a multidirectional array using some examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.