23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1 Data Structures<br />

Avoiding Ambiguity in Multidimensional Indexing<br />

Some assignment statements, such as<br />

A(:,:,2) = 1:10<br />

are ambiguous because they do not provide enough information about the<br />

shape of the dimension to receive the data. In the case above, the statement<br />

tries to assign a one-dimensional vector to a two-dimensional destination.<br />

<strong>MATLAB</strong> produces an error for such cases. To resolve the ambiguity, be sure<br />

you provide enough information about the destination for the assigned data,<br />

and that both data and destination have the same shape. For example:<br />

A(1,:,2) = 1:10;<br />

Reshaping Multidimensional Arrays<br />

Unless you change its shape or size, a <strong>MATLAB</strong> array retains the dimensions<br />

specified at its creation. You change array size by adding or deleting<br />

elements. You change array shape by respecifying the array’s row, column, or<br />

pagedimensionswhileretainingthesameelements. Thereshape function<br />

performs the latter operation. For multidimensional arrays, its form is<br />

B = reshape(A,[s1 s2 s3 ...])<br />

s1, s2, and so on represent the desired size for each dimension of the reshaped<br />

matrix. Note that a reshaped array must have the same number of elements<br />

as the original array (that is, the product of the dimension sizes is constant).<br />

M reshape(M, [6 5])<br />

The reshape function operates in a columnwise manner. It creates the<br />

reshaped matrix by taking consecutive elements down each column of the<br />

original data construct.<br />

1-62

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

Saved successfully!

Ooh no, something went wrong!