Programming |
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
Note If you intend to expand the size of a matrix repeatedly over time as it requires more room (usually done in a programming loop), it is advisable to preallocate space for the matrix when you initially create it. See Preallocating Memory. |
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
:
col = 10; comp04 = []; for k = 1:50 t = compareResults(stats04(k,1:col), stats03(k,1:col)); comp04 = [comp04; t]; end
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:
A = [ 10 20 30 40 50; ... 60 70 80 90 100; ... 110 120 130 140 150]; A = [A; 160] ??? Error using ==> vertcat All rows in the bracketed expression must have the same number of 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:
Note Attempting to read from nonexistent matrix locations generates an error. You can only write to these locations. |
You can also expand the matrix by adding a matrix instead of just a single element:
A(4:6,1:3) = magic(3)+100 A = 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 108 101 106 0 0 103 105 107 0 0 104 109 102 0 0
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:
A(4,8) = 300 A = 10 20 30 40 50 0 0 0 60 70 80 90 100 0 0 0 110 120 130 140 150 0 0 0 0 0 0 0 0 0 0 300
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.
Add a cell to C{3,1}
and MATLAB appends an entire row:
C{3, 1} = ... struct('Fund_A', .45, 'Fund_E', .35, 'Fund_G', 20); C = 'Madison' 'G' [1x3 double] [ 46] '325 Maple Dr' [3.0153e+003] [1x1 struct] [] []
Data Structures Used in the Matrix | Diminishing the Size of a Matrix |
© 1994-2005 The MathWorks, Inc.