Using the C++ Math Library | ![]() ![]() |
Using the Operators
Many of MATLAB's mathematical operators (+
, -
, *
, /
, ^
) are the same as those available in C++. The exceptions are ', \,
and the array operators .*
, ./
, .\,
and .^
, because the syntax of C++ does not support their definition as operators. You must use the functional equivalents provided by the MATLAB C++ Math Library to perform these operations.
This table demonstrates how the library supports mathematical operators. Note that the library also provides functional equivalents for the set of operators that are supported by C++ syntax.
With the exception of the unary transpose()
and ctranspose()
functions, the C++ functions in the table take two matrix arguments and return a third matrix. To see these functions in action, consider the C++ translation of the MATLAB code presented on page 6-2 at the beginning of this section. Function calls replace the use of operators. (Note that *
can be used instead of mtimes
.)
static double data[] = { 1, 3, 2, 4 }; mwArray A(2, 2, data); mwArray B = eye(2); // 2x2 identity matrix mwArray C = mtimes(A, B); // Matrix multiplication cout << C << endl; mwArray D = times(A, B); // Array multiplication cout << D << endl;
Running this code fragment produces:
[ 1 2 3 4 ] [ 1 0 0 4 ]
Use the other binary operator functions in a similar manner.
![]() | Using the Mathematical Operators | Defining Your Own Operators | ![]() |