Using the C++ Math Library    

MATLAB for C++ Programmers

If you're a C++ programmer who has never used MATLAB, make sure you understand the previous section before reading this one. The MATLAB language isn't complicated, but there are important differences between it and C++. These differences won't affect your use of this product directly, because you will, after all, be using C++, but if you understand the differences, you will have a better understanding of the constraints that guided the design of the MATLAB C++ Math Library.

The major differences between MATLAB and C++ include:

MATLAB is a more specialized programming language than C++ and, as a result, lacks much of the machinery of C++, such as typed variables and name space management. MATLAB is not a general-purpose programming language like C++, so it is not nearly as versatile as C++. However, in its domain, numerical linear algebra, MATLAB is far easier to use and much more concise than C++. This library brings some of that power to C++ programmers.

Much of MATLAB's expressive power stems from its rich collection of numerical operators. In C++ it is impossible to emulate perfectly MATLAB's operator syntax, because some of the MATLAB operators like .* consist of two characters, while others like 'are not legal C++ operators. However, many of MATLAB's operators are present in C++, overloaded to provide commutativity and inlined for efficiency. Those MATLAB operators that are not present as C++ operators are available as function calls.

Fortunately, one of MATLAB's most powerful operators, (), for array indexing, is a valid C++ operator. The indexing operator can access a single element or a group of elements in an array. For example, in MATLAB, A(2:4, 1:3) returns a 3-by-3 array consisting of the second, third and fourth elements in the first three columns of array A. Because the : is not a valid C++ operator, the equivalent expression in C++ requires the use of the colon() function: A(colon(2,4), colon(1,3)). The indexing operator is also the only operator that can modify an array. For example, the expression

writes the value 13 into the entry at row 4 and column 7 of array A. This is the only way to modify the contents of an array. Because the MATLAB cell array indexing operator, {}, is not a valid C++ operator, the MATLAB C++ Math Library uses the cellhcat() routine to emulate it.

You have seen that an array subscript can itself be an array. When an array subscript is a logical array containing only zeros and ones, it is called a logical index. A logical index acts like a mask or filter. Each element in the logical index corresponds to an element in the subscripted array. If the element in the logical index is 1, the corresponding element in the subscripted array appears in the result. The result of any nontrivial (array of all ones or all zeros) logical indexing operation can have various shapes depending on the shape of the indices.

See Differences Between C++ and MATLAB for a more comprehensive list of differences between MATLAB and C++. See MATLAB C++ Math Library Basics to learn how these principles are expressed in the MATLAB C++ Math Library.


 Flow of Control C++ for MATLAB Users