Programming Previous page   Next Page

Resizing and Reshaping Matrices

You can easily enlarge or shrink the size of a matrix, modify its shape, or rotate it about various axes. This section covers

Expanding the Size of a Matrix

You can expand the size of any existing matrix as long as doing so does not give the resulting matrix an irregular shape. (See Keeping Matrices Rectangular). For example, you can vertically combine a 4-by-3 matrix and 7-by-3 matrix because all rows of the resulting matrix have the same number of columns (3).

Two ways of expanding the size of an existing matrix are

Concatenating Onto the Matrix

Concatenation is most useful when you want to expand a matrix by adding new elements or blocks that are compatible in size with the original matrix. This means that the size of all matrices being joined along a specific dimension must be equal along that dimension. See Concatenating Matrices.

This example runs a user-defined function compareResults on the data in matrices stats04 and stats03. Each time through the loop, it concatenates the results of this function onto the end of the data stored in comp04:

Concatenating to a Structure or Cell Array.   You can add on to arrays of structures or cells in the same way as you do with ordinary matrices. This example creates a 3-by-8 matrix of structures S, each having 3 fields: x, y, and z, and then concatenates a second structure matrix S2 onto the original:

Create a 3-by-8 structure array S:

Create a second array that is 3-by-2 and uses the same field names:

Concatenate S2 onto S along the horizontal dimension:

Adding Smaller Blocks to a Matrix

To add one or more elements to a matrix where the sizes are not compatible, you can often just store the new elements outside the boundaries of the original matrix. MATLAB automatically pads the matrix with zeros to keep it rectangular.

Construct a 3-by-5 matrix, and attempt to add a new element to it using concatenation. The operation fails because you are attempting to join a one-column matrix with one that has five columns:

Try this again, but this time do it in such a way that enables MATLAB to make adjustments to the size of the matrix. Store the new element in row 4, a row that does not yet exist in this matrix. MATLAB expands matrix A by an entire new row by padding columns 2 through 5 with zeros:

You can also expand the matrix by adding a matrix instead of just a single element:

You do not have to add new elements sequentially. Wherever you store the new elements, MATLAB pads with zeros to make the resulting matrix rectangular in shape:

Expanding a Structure or Cell Array.   You can expand a structure or cell array in the same way that you can a matrix. This example adds an additional cell to a cell array by storing it beyond the bounds of the original array. MATLAB pads the data structure with empty cells ([]) to keep it rectangular.

The original array is 2-by-3:

Add a cell to C{3,1} and MATLAB appends an entire row:

Previous page  Data Structures Used in the Matrix Diminishing the Size of a Matrix Next page

© 1994-2005 The MathWorks, Inc.