Using the C++ Math Library    

The mwIndex Class

mwArray overloads the () operator for MATLAB-like indexing. The mwArray::operator() functions can accept mwIndex objects. An mwIndex can represent a single integer, a sequence of integers specified by a tuple (start, step, stop), or an arbitrary vector of integers. Array subscripts are always integers; the MATLAB C++ Math Library truncates floating-point numbers to integers in indexing expressions.

You can create mwIndex objects in several ways. The way most familiar to MATLAB users is the colon() function. An mwIndex constructor exists for each form of the colon() function, as this table demonstrates.

Table 4-2: mwIndex Class and the colon() Function 
Description
MATLAB Expression
C++ colon() Equivalent
mwIndex Constructor
All elements of X
X(:)
X(colon())
mwIndex k;
X(k)
First 10 elements of row 1
X(1,1:10)
X(1,colon(1,10))
mwIndex k(1,10);
X(k)
Elements 2,4,6,8,10 of X
X(2:2:10)
X(colon(2,2,10))
mwIndex k(2,2,10);
X(k)


 C++ and MATLAB Indexing Syntax Programming Efficient Indices