Using the C Math Library    

Extending Two-Dimensional Indexing to N Dimensions 

Two-dimensional indexing extends very naturally to N dimensions; simply use more index arguments. Let A be a 3-by-3-by-2 three-dimensional array (two 3-by-3 pages):

Page 1:

Page 2:

Then the MATLAB expression A(:,:,2) selects all of page 2, A(1,:,:) selects all the columns in row 1 on all the pages, A(2,2,2) selects the element at the middle of page 2 (the number 14), and so on.

It is very simple to convert these MATLAB indexing expressions into MATLAB C Math Library indexing expressions:

A(:,:,2) becomes

The result of this operation is the 3-by-3 array on page 2 of A:

A(1,:,:) becomes

The result of this operation is a three-dimensional array 1-by-3-by-2 in which each "page" consists of the first row of the corresponding page of A.

Page 1:

Page 2:

Finally, A(2,2,2) becomes:

The result of this operation is the 1-by-1 array 14.

If the array A had more than three dimensions, the index strings would have more than three ?'s in them, and they would be followed by more than three index values. All of the other types of indexing discussed in this chapter (selecting entire rows and columns, etc.) work equally well on N-dimensional arrays.


 Selecting a Matrix Logical Indexing