Using the C Math Library | ![]() ![]() |
Assumptions for the Code Examples
The C code included in the following sections demonstrates how to perform indexing with the MATLAB C Math Library. For the most part, each example only presents the call to an indexing function. As you read the examples, assume that the code relies on declarations, assignments, and deletions that follow these conventions:
mlfEnterNewContext()
and end with calls to mlfRestorePreviousContext()
and mlfReturnValue()
. Assignments are made by calling mlfAssign()
.mlfDoubleMatrix()
function. For example, this code creates matrix A
:static double A_array_data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; mxArray *A; mlfAssign(&A, mlfDoubleMatrix(3, 3, A_array_data, NULL));
See Example Program: Creating Numeric Arrays (ex1.c) in Chapter 3 for a complete example of how to use this function.
A
, which is used throughout the examples, is equal to:1 4 7 2 5 8 3 6 9
Note how the value of each element in A
is equal to that element's position in the column-major enumeration order. For example, the third element of A
is the number 3 and the ninth element of A
is the number 9.
mlfScalar()
create the arrays that contain the indices. Because automatic memory management is in effect, these scalar arrays are automatically deleted by the library because they are temporary arrays.mxArray
that is the target of an assignment must be deleted after the program finishes with it.mxDestroyArray(A);
mlfHorzcat()
and mlfVertcat()
functions to create the vectors and matrices that are used as indices. mlfHorzcat()
concatenates its arguments horizontally; mlfVertcat()
concatenates its arguments vertically.Refer to the online MATLAB C Math Library Reference for more information on mlfScalar()
, mlfCreateColonIndex()
, mxCreateDoubleMatrix()
, mxGetPr()
, mlfHorzcat()
, and mlfVertcat()
.
![]() | Specifying a Source Array for Assignments | One-Dimensional Indexing | ![]() |