2.2 Multidimensional Arrays

MATLAB supports multidimensional arrays. Here we present just some of the MATLAB capabilities for such arrays; to obtain more information, type help data types. A three-dimensional array has the dimension m x 11 x q.   four- dimensional array has the dimension m x n x q x r , and so forth. The first two  dimensions are the row and column, as with a matrix. The higher dimensions are called pages. The elements of a matrix are specified by two indices; three indices  re required to specify an element of a three-dimensional array, and so on. You  can think of a

three-dimensional array as layers of matrices. The first layer is page I; the second layer is page 2, and so on. If A is a 3 x 3 x 2 array, you can access the element in row 3, column 2 of page 2 by typing A (3 , 2 , 2 ) . To access all of page I, type A ( 1)  To access all of page 2, type A( 2 ).The ndims command returns the number of dimensions. For example, for the array A just described, ndims (A) returns the value 3. You can create a multidimensional array by first creating a two-dimensional array and then extending it. For example, suppose you want to create a three dimensional array whose ‘first page is


To do so, first create page 1 as a 3 x 3 matrix and then add page 2, as follows:

»A = [4,6,1;5,8,0;3,9,2];
»A ( : , : , 2) = [6,2,9; 0,3,1; 4,7,5]

MATLAB displays the following:
A{ 1) =

4 6 1
5 8 0

3 9 2

A{ 2) =

 6 2 9
0 3 1

 4 7 5

Another way to produce such an array is with the ca t command. Typing cat (n, A, B, C, … ) creates a new array by concatenating the arrays A, B, C, and so on along the dimension n. Note that cat (1, A, B) is the same as [A; B) and that ca t (2 , A, B) is the same as [A, B) . For example, suppose we have the 2 x 2 arrays A and B:


Then C = cat (3 I AI B) produces a three-dimensional array. We can think of this array as composed of two layers; the first layer is the matrix A, and the second layer is the matrix B. The element C (m In, p) is located in row m, column n, and layer p. Thus the element C (2 11,1) is 9, and the element C (2 12,2) is 3. This function is summarized in Table 2.1-1.

Multidimensional arrays are useful for problems that involve several parameters. For example, if we have data on the temperature distribution in a rectangular object. we could represent the temperatures as an array T with three dimensions. Each temperature would correspond to the temperature of a rectangular block within the object, and the array indices would correspond to x, y, z locations within the object. For example, T (2 , 4 , 3) would be the temperature in the blocklocated in row 2, column 4;page 3 and having the coordinates X2, Y4, Z3.

Share This