Reshape function is used to give a new shape to the array with a specified number of rows and columns. The reshaped array should be compatible with the original array. It is used in both Python and Matlab to execute various operations in the array. After reshaping the array, it adjusts the memory allocation accordingly. Reshaping can be in the form of increasing or decreasing the dimension of the array. These functions are used in many fields like data science or image modification etc.
Working of Reshape Function in Matlab
There are many operations that can be performed using one dimensional or two-dimensional arrays. Reshape function in Matlab is used to modify the original or existing array into a different array with different dimensions or sequence.
There are various syntax which is used in Matlab like:
R=reshape (X, size)
This function is used to reshape the original matrix that is X into R with the size defined in the vector ‘size’. The vector should contain at least 2 elements in it.
For example, if its reshape (X, [1,3]): it will reshape X into a 1 by 3 matrix.
R=reshape (X, size 1, size 2…. size n)
This function is used to reshape A into size specified where size1, size2 indicates each dimension present. We can specify the dimension as  which will calculate the dimension automatically in such a way that the number of elements present in the reshaped array matches with the total count of elements in the input array. The input array i.e. ’X’ can be vector, matrix or multi-dimensional array and the data types that are supported are single, double, int16, int8, int32, int64, unit8, uint16, unit32, unit64, char, string, logical, date time, duration. It also supports complex numbers.
The size of the resultant array should be specified with a vector of integers and the size should be in such a way that the number of elements should be the same with the original array. We should provide at least 2 dimension sizes and if there is at most one dimension then they can be specified as . The output or resultant should be vector, matrix, multidimensional-array and cell array. The data types and the number of elements in the reshaped array should always be the same as that of the original array. The data types that are supported by the reshaped array are single, double, int16, int8, int32, int64, unit8, uint16, unit32, unit64, char, string, logical, date time, duration. We can also reshape the symbolic array present in Matlab.
There are different syntaxes for these:
R=reshape (X, y1, y2)
This is used to return y1 by y2 matrix which has an equal number of elements as that of X. The elements that are present in the resultant matrix are taken column-wise from the given array ‘X’.
R=reshape (X, y1, y2…yn)
This is used to return y1 by …yn matrix which has an equal number of elements as that of X. The elements that are present in the resultant matrix are taken column-wise from the given array ‘X’. In the above syntax, the input array is X which can be a symbolic vector, symbolic matrix or symbolic multi-dimensional array. y1 and y2 represent the dimension of the reshaped matrix which are separated by commas and the number of elements should be the same as that of the original array.
For example, reshape(X,2,3) gives the reshaped array as 2 by 3 matrix. If there is more dimension, then they are represented in the same way.
For example: reshape(X,2,3,3) results in a 2 by 3 by 3 matrix.
Examples of Reshape in Matlab
Given below are the examples of Reshape in Matlab:
To reshape a 11:20 vector into a 2 by 5 matrix
X = 11:20; R = reshape(X,[2,5])
To reshape the matrix with the specified number of columns in the input arguments.
X = magic(6)
After reshaping the above matrix with specifying the number of columns as 3 and we have given  instead of a total number of rows so that it calculates the number of rows automatically. Please find the below matrix after applying the required function:
R = reshape(X,,3)
The reshaped matrix is a 12 by 3 matrix which 12 number of rows and 3 columns which has the same number of elements as the original array X i.e. 36.
To reshape a matrix into 3 by 4 matrix.
X = [1 2 4 7 6 1; 2 9 4 1 7 3]; R = reshape(X,3,4)
After reshaping the above matrix into a 3 by 4 matrix, we get the desired matrix and it has the same number of elements as that of the original array.
To use reshape function with multi-dimensional array and convert them into a 4 by 3 matrix. The input array is a multi-dimensional array of dimensions 2 by 3 by 2.
X = zeros(2,3,2); R = reshape(X,4,3)