23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Resizing and Reshaping Matrices<br />

Resizing and Reshaping Matrices<br />

You can easily enlarge or shrink the size of a matrix, modify its shape, or<br />

rotate it about various axes. This section covers<br />

• “Expanding the Size of a Matrix” on page 1-27<br />

• “Diminishing the Size of a Matrix” on page 1-31<br />

• “Reshaping a Matrix” on page 1-32<br />

• “Preallocating Memory” on page 1-34<br />

Expanding the Size of a Matrix<br />

You can expand the size of any existing matrix as long as doing so does<br />

not give the resulting matrix an irregular shape. (See “Keeping Matrices<br />

Rectangular” on page 1-8). For example, you can vertically combine a 4-by-3<br />

matrix and 7-by-3 matrix because all rows of the resulting matrix have the<br />

same number of columns (3).<br />

Twowaysofexpandingthesizeofanexistingmatrixare<br />

• Concatenating new elements onto the matrix<br />

• Storing to a location outside the bounds of the matrix<br />

Note If you intend to expand the size of a matrix repeatedly over time<br />

as it requires more room (usually done in a programming loop), it is<br />

advisable to preallocate space for the matrix when you initially create it. See<br />

“Preallocating Memory” on page 1-34.<br />

Concatenating Onto the Matrix<br />

Concatenation is most useful when you want to expand a matrix by adding<br />

newelementsorblocksthatarecompatible in size with the original matrix.<br />

This means that the size of all matrices being joined along a specific dimension<br />

must be equal along that dimension. See “Concatenating Matrices” on page<br />

1-7.<br />

1-27

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!