Using the C++ Math Library    

C++ and MATLAB Indexing Syntax

The table below summarizes differences between C++ and MATLAB standard array indexing syntax. Although the MATLAB C++ Math Library provides the same functionality as the MATLAB interpreter, the syntax of some operations is slightly different. In particular, you must use the colon() function rather than the colon operator.

Though not listed here, you must use mwArray.cell() rather than {} and mwArray::field() rather than the period (.) that references a structure field.

Example Matrix X

Table 4-1: MATLAB/C++ Indexing Expression Equivalence 
Description
MATLAB Expression
C++ Expression
Result
Extract 1,1 element
X(1,1)
X(1,1)
4
Extract first element
X(1)
X(1)
4
Extract third element
X(3)
X(3)
5
Extract all elements into a column vector
X(:)
X(colon())
4
6
5
7
Extract first row
X(1,:)
X(1,colon())
4 5
Extract second row
X(2,:)
X(2,colon())
6 7
Extract first column
X(:,1)
X(colon(), 1)
4
6
Extract second column
X(:,2)
X(colon(), 2)
5
7
Replace first element
with 9
X(1) = 9
X(1) = 9
9 5
6 7
Replace first row with
[ 11 12 ]
X(1,:) = [ 11 12 ]
X(1,colon())=     horzcat(11,12);
11 12
6 7
Replace element 2,1 with 9
X(2,1) = 9
X(2,1) = 9;
4 5
9 7
Replace elements 1 and 4 with 8 (one-dimensional indexing)
X([1 4]) = [ 8 8 ]
X(horzcat(1,4))=     horzcat(8, 8);
8 5
6 8


 Concatenating Subscripts The mwIndex Class