Using the C Math Library    

Comparison of C and MATLAB Indexing Syntax 

The table below summarizes the differences between the MATLAB and C indexing syntax. Although the MATLAB C Math Library provides the same functionality as the MATLAB interpreter, the syntax is very different. Refer to Assumptions for the Code Examples to look up the conventions used for the code within the table.

Example Matrix X

Table 5-2: MATLAB/C Indexing Expression Equivalence 
Description
MATLAB Expression
C Expression
Result
Extract 1,1 element
X(1,1)
mlfIndexRef(
   X,
   "(?,?)",
   mlfScalar(1),
   mlfScalar(1)
   )
4
Extract first element
X(1)
mlfIndexRef(
   X,
   "(?)",
   mlfScalar(1)
   )
4
Extract third element
X(3)
mlfIndexRef(
   X,
   "(?)",
   mlfScalar(3)
   )
5
Extract all elements into column vector
X(:)
mlfIndexRef(
   X,
   "(?)",
   mlfCreateColonIndex()
   )
4
6
5
7
Extract first row
X(1,:)
mlfIndexRef(
   X,
   "(?,?)",
   mlfScalar(1),
   mlfCreateColonIndex()
   )
4 5
Extract second row
X(2,:)
mlfIndexRef(
   X,
   "(?,?)",
   mlfScalar(2),
   mlfCreateColonIndex()
   )
6 7
Extract first column
X(:,1)
mlfIndexRef(
   X,
   "(?,?)",
   mlfCreateColonIndex(),
   mlfScalar(1)
   )
4
6
Extract second column
X(:,2)
mlfIndexRef(
   X,
   "(?,?)",
   mlfCreateColonIndex(),
   mlfScalar(2)
   )
5
7
Replace first element
with 9
X(1) = 9
mlfIndexAssign(
   &X,
   "(?)",
   mlfScalar(1),
   mlfScalar(9)
   );
9 5
6 7
Replace first row with
[11 12]
X(1,:) = [ 11 12 ]
mlfIndexAssign(
   &X,
   "(?,?)",
   mlfScalar(1),
   mlfCreateColonIndex(),
   mlfHorzcat(
      mlfScalar(11),
      mlfScalar(12),
      NULL)
   );
11 12
6 7
Replace element 2,1 with 9
X(2,1) = 9
mlfIndexAssign(
   &X,
   "(?,?)",
   mlfScalar(2),
   mlfScalar(1),
   mlfScalar(9)
   );
4 5
9 7
Replace elements 1 and 4 with 8 (one-dimensional indexing)
X([1 4]) = [ 8 8 ]
mlfIndexAssign(
   &X,
   "(?)",
   mlfHorzcat(
      mlfScalar(1),
      mlfScalar(4),
      NULL),
   mlfHorzcat(
      mlfScalar(8),
      mlfScalar(8),
      NULL)
   );
8 5
6 8


 Deleting Elements from a Structure Array Calling Library Routines