Programming Previous page   Next Page

Reshaping Multidimensional Arrays

Unless you change its shape or size, a MATLAB array retains the dimensions specified at its creation. You change array size by adding or deleting elements. You change array shape by respecifying the array's row, column, or page dimensions while retaining the same elements. The reshape function performs the latter operation. For multidimensional arrays, its form is

s1, s2, and so on represent the desired size for each dimension of the reshaped matrix. Note that a reshaped array must have the same number of elements as the original array (that is, the product of the dimension sizes is constant).

M
reshape(M, [6 5])

The reshape function operates in a columnwise manner. It creates the reshaped matrix by taking consecutive elements down each column of the original data construct.

C
reshape(C, [6 2])

Here are several new arrays from reshaping nddata:

Removing Singleton Dimensions

MATLAB creates singleton dimensions if you explicitly specify them when you create or reshape an array, or if you perform a calculation that results in an array dimension of one:

The squeeze function removes singleton dimensions from an array:

The squeeze function does not affect two-dimensional arrays; row vectors remain rows.


Previous page  Indexing Multidimensional Arrays Permuting Array Dimensions Next page

© 1994-2005 The MathWorks, Inc.