4.3 Reshape Function

The reshape function is used when you want to reshape a matrix.

The general syntax for the reshape function is:
    reshape(A, sz1, sz2)
Here, A represents the matrix,  sz1 is the size of the first dimension, and sz2 is the size of the second dimension.

Play media comment.

An important example: 
Lets say that we have a matrix B, such that size(B) =  800 x 20, i.e. B has 800 rows and 20 columns. 

Now, let's say you would like to reshape this matrix to a matrix having 40 columns instead. Well, how many rows must we then have to include all elements? A short calculation shows that we now must have 400 observations in each row. Hence, we could type the following in MATLAB: 

>> reshape(B, 400, 40)  %works since 800 x 20 = 16 000 which is the same as 400 x 40 = 16 000. 

This was quite straight forward. 

Now let's say that you have a matrix C, such that size(C) = 1449 x 5, i.e. C has 1449 rows and 5 columns. 

We would like to have 45 columns instead. How many rows must you include? This is quite tedious to do "by hand".  The solution is:

>> reshape(C, [], 45).   %the new size is 161 x 45. Verify that 161 x 45 = 1449 x 5 = 7245.

What MATLAB did is that when you specified [] for a dimension, then it automatically calculates the suitable dimension. If you would type:

>> reshape(C, [], 46) 

This would produce an error. To understand why this produces an error, consider we are asking the function to do!