MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

1 Data Structures As you add dimensions to an array, you also add subscripts. A four-dimensional array, for example, has four subscripts. The first two reference a row-column pair; the second two access the third and fourth dimensions of data. Note The general multidimensional array functions reside in the datatypes directory. Creating Multidimensional Arrays You can use the same techniques to create multidimensional arrays that you use for two-dimensional matrices. In addition, MATLAB provides a special concatenation function that is useful for building multidimensional arrays. This section discusses • “Generating Arrays Using Indexing” on page 1-54 • “Extending Multidimensional Arrays” on page 1-55 • “Generating Arrays Using MATLAB Functions” on page 1-56 • “Building Multidimensional Arrays with the cat Function” on page 1-56 Generating Arrays Using Indexing One way to create a multidimensional array is to create a two-dimensional array and extend it. For example, begin with a simple two-dimensional array A. A = [5 7 8; 0 1 9; 4 3 6]; A is a 3-by-3 array, that is, its row dimension is 3 and its column dimension is 3. To add a third dimension to A, A(:,:,2) = [1 0 4; 3 5 6; 9 8 7] MATLAB responds with A(:,:,1) = 5 7 8 0 1 9 1-54

Multidimensional Arrays 4 3 6 A(:,:,2) = 1 0 4 3 5 6 9 8 7 You can continue to add rows, columns, or pages to the array using similar assignment statements. Extending Multidimensional Arrays To extend A in any dimension: • Increment or add the appropriate subscript and assign the desired values. • Assign the same number of elements to corresponding array dimensions. For numeric arrays, all rows must have the same number of elements, all pages must have the same number of rows and columns, and so on. You can take advantage of the MATLAB scalar expansion capabilities, together with the colon operator, to fill an entire dimension with a single value: A(:,:,3) = 5; A(:,:,3) ans = 5 5 5 5 5 5 5 5 5 To turn A into a 3-by-3-by-3-by-2, four-dimensional array, enter A(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9]; A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1]; A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1]; Note that after the first two assignments MATLAB pads A with zeros, as needed, to maintain the corresponding sizes of dimensions. 1-55

Multidimensional Arrays<br />

4 3 6<br />

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

1 0 4<br />

3 5 6<br />

9 8 7<br />

You can continue to add rows, columns, or pages to the array using similar<br />

assignment statements.<br />

Extending Multidimensional Arrays<br />

To extend A in any dimension:<br />

• Increment or add the appropriate subscript and assign the desired values.<br />

• Assign the same number of elements to corresponding array dimensions.<br />

For numeric arrays, all rows must have the same number of elements, all<br />

pages must have the same number of rows and columns, and so on.<br />

You can take advantage of the <strong>MATLAB</strong> scalar expansion capabilities,<br />

together with the colon operator, to fill an entire dimension with a single value:<br />

A(:,:,3) = 5;<br />

A(:,:,3)<br />

ans =<br />

5 5 5<br />

5 5 5<br />

5 5 5<br />

To turn A into a 3-by-3-by-3-by-2, four-dimensional array, enter<br />

A(:,:,1,2) = [1 2 3; 4 5 6; 7 8 9];<br />

A(:,:,2,2) = [9 8 7; 6 5 4; 3 2 1];<br />

A(:,:,3,2) = [1 0 1; 1 1 0; 0 1 1];<br />

Note that after the first two assignments <strong>MATLAB</strong> pads A with zeros, as<br />

needed, to maintain the corresponding sizes of dimensions.<br />

1-55

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

Saved successfully!

Ooh no, something went wrong!