
Introduction to MATLAB and Octave 489
The
reshape function produces a matrix with elements taken column by column from the
given matrix. Thus:
MATLAB/Octave
>> c = [1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 17 18 19 20]
c =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
>> reshape(c,2,10)
ans =
1 11 2 12 3 13 4 14 5 15
6 16 7 17 8 18 9 19 10 20
>> reshape(c,5,4)
ans =
1 7 13 19
6 12 18 5
11 17 4 10
16 3 9 15
2 8 14 20
Reshape produces an error if the product of the two values is not equal to the number of
elements of the matrix. Note that we could have produced the original matrix above with
MATLAB/Octave
>> c = reshape([1:20],5,4])’
All these commands work equally ...