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.

1 Data Structures<br />

Generating Arrays Using <strong>MATLAB</strong> Functions<br />

You can use <strong>MATLAB</strong> functions such as randn, ones, andzeros to generate<br />

multidimensional arrays in the same way you use them for two-dimensional<br />

arrays. Each argument you supply represents the size of the corresponding<br />

dimension in the resulting array. For example, to create a 4-by-3-by-2 array of<br />

normally distributed random numbers:<br />

B = randn(4,3,2)<br />

To generate an array filled with a single constant value, use the repmat<br />

function. repmat replicates an array (in this case, a 1-by-1 array) through a<br />

vector of array dimensions.<br />

B = repmat(5, [3 4 2])<br />

B(:,:,1) =<br />

5 5 5 5<br />

5 5 5 5<br />

5 5 5 5<br />

B(:,:,2) =<br />

5 5 5 5<br />

5 5 5 5<br />

5 5 5 5<br />

Note Any dimension of an array can have size zero, making it a form of empty<br />

array. For example, 10-by-0-by-20 is a valid size for a multidimensional array.<br />

Building Multidimensional Arrays with the cat Function<br />

The cat function is a simple way to build multidimensional arrays; it<br />

concatenates a list of arrays along a specified dimension:<br />

B = cat(dim, A1, A2...)<br />

where A1, A2, and so on are the arrays to concatenate, and dim is the<br />

dimension along which to concatenate the arrays.<br />

For example, to create a new array with cat:<br />

1-56

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

Saved successfully!

Ooh no, something went wrong!