Using the C Math Library    

Selecting a Vector of Elements 

Use one vector and one scalar index, or one matrix and one scalar index, to extract a vector of elements from an array. You can use the functions mlfHorzcat(), mlfVertcat(), or mlfCreateColonIndex() to make the vector or matrix index, or use an mxArray variable that contains a vector or matrix returned from other functions.

The indexing routines iterate over the vector index or down the columns of the matrix index, pairing each element of the vector or matrix with the scalar index. Think of this process as applying a (scalar, scalar) subscript multiple times; the result of each selection is collected into a vector.

For example,

selects the first and third element (or first and third rows) of column 2:

In MATLAB A([1 3], 2) performs the same operation.

If you reverse the positions of the indices (A(2, [1 3]) in MATLAB):

you select the first and third elements (or first and third columns) of row 2:

If the vector index contains the same number multiple times, the same element is extracted multiple times. For example,

returns two copies of the element at A(2,3):

Specifying a Vector Index with mlfEnd( )

The mlfEnd() function, which corresponds to the MATLAB end() function, provides another way of specifying a vector index. Given an array, a dimension (1 = row , 2 = column, 3 = page, and so on), and the number of indices in the subscript, mlfEnd() returns the index of the last element in the specified dimension. You then use that scalar array to generate a vector index. See Specifying a Vector Index with mlfEnd() for a more complete description of how and why you use the end function in MATLAB.

Given the row dimension, mlfEnd() returns the number of columns. Given the column dimension, it returns the number of rows. The number of indices in the subscript corresponds to the number of index arguments you pass to mlfIndexRef().

This code selects all but the first element in row 3, just as

does in MATLAB.

The second argument to mlfEnd(), two, identifies the dimension where mlfEnd() is used, here the column dimension. The third argument, two, indicates the number of indices in the subscript; for two-dimensional indexing, it is always two. This code selects these elements from matrix A:

Selecting a Row or Column

Use a colon index and a scalar index to select an entire row or column. For example,

selects the first row:

mlfIndexRef(A, "(?,?)", mlfCreateColonIndex(), mlfScalar(2)) selects the second column:


 Selecting a Single Element Selecting a Matrix