Using the C++ Math Library    

Using the Mathematical Operators


Overview

MATLAB supports two types of mathematical operators: array operators that operate on individual elements of a matrix and matrix operators that operate on whole matrices. In MATLAB, array operators begin with a . (period). Matrix operators do not. For example, .* is the array multiplication operator and * is the matrix multiplication operator.

Array operators treat the elements of each operand individually. Given two operands A and B, an array operator op computes a result C, such that
C(i,j) = A(i,j) op B(i,j). The matrices A, B, and C are all the same size.

Matrix operators perform more complex computations. Often the value of an element C(i,j) in the result depends on the values of multiple elements in each input matrix. No single rule describes the relationship between input and output elements for matrix operators. For example, in a matrix multiplication such as C = A * B, the value C(i,j) depends on all of the values in row i of matrix A and column j of matrix B.

This MATLAB code demonstrates the difference between array and matrix multiplication. Note that this is not C++ code.

First, initialize two matrices:

Now compute the array product (array multiplication):

Now compute the matrix product (matrix multiplication):

After this MATLAB code is executed, the matrix C contains the array product [ 1 0; 0 4 ]. Since C was computed by array multiplication, the elements of C equal:

The matrix D, on the other hand, contains the linear-algebraic product of A with the identity matrix: A itself. The equivalent C++ code is presented at the end of this section on page 6-5.


 Representing Input Arguments As a Cell Array Using the Operators