Math Operators in MATLAB
Basic Mathematical Operators for Scalars
When performing calculations with scalars, MATLAB works just like a (very powerful) calculator. MATLAB has specific characters to perform mathematical operations.
Operation | Example Math Expression | MATLAB Command |
---|---|---|
Addition | 3 + 3 | 3 + 3 |
Subtraction | 3 – 3 | 3 - 3 |
Negation | -1 | -1 |
Multiplication | 3 * 3 | 3 * 3 |
Division | 15 รท 3 | 15 / 3 |
Exponentiation | 32 | 3^2 |
Math with Vectors and Matrices in MATLAB
MATLAB stands for MATrix LABoratory. Its default setting is to perform mathematical operations according to the rules of linear algebra, which focuses on mathematics using vectors and matrices. Before you perform multiplication, division, or exponentiation with vectors or matrices in MATLAB, it is important to know that MATLAB allows you to differentiate between matrix math and element-by-element math operations.
MATLAB performs matrix math by default. An example of matrix multiplication:
MATLAB can also perform element-wise operations, which is when an operation is performed between each element and only its counterpart in the other corresponding array(s). The terms array operations, element-wise operations, and element-by-element operations all mean the same thing in this course. An example of element-wise multiplication:
When performing calculations using vectors or matrices, you must tell MATLAB which type of math it should use. You do this by using the proper operator. Element-wise operators use the standard MATLAB math operator preceded by a period character.
Operator Comparison
Operation | Element-wise Operator | Matrix Math Operator |
---|---|---|
Addition | + | + |
Subtraction | - | - |
Negation | - | - |
Multiplication | .* | * |
Division | ./ | / |
Exponentiation | .^ | ^ |
In this course, you will only code element-wise operations unless specifically told otherwise, and the arrays used in element-wise operations must have the same dimensions and the resulting array will have the same dimension as the operand arrays.
For addition and subtraction, there is no period character regardless of the array type. When working with scalars only, element-wise and matrix operators produce the same result so avoid using the unnecessary element-wise operators. When you have a mix of scalars and vectors/matrices, then you need to manage the computations with appropriate period characters to ensure you get the proper results.
Element-wise Operations Reference Table
Element-wise Operation | Format | MATLAB Example(s) |
---|---|---|
Addition | A + B | [1 0 5] + [1 3 2] = [2 3 7] |
Subtraction | A – B | [1 0 5] - [1 3 2] = [0 -3 3] |
Multiplication of a scalar and an array | a * B | 3 * [3 5 4] = [9 15 12] |
Multiplication of arrays | A .* B | [8 1 5] .* [2 3 4] = [16 3 20] |
Division of an array by a scalar | A / b | [16 10 22] / 2 = [8 5 11] |
Division of arrays | A ./ B | [24 25 35] ./ [8 5 7] = [3 5 5] |
Division of a scalar by an array | a ./ B | 12 ./ [4 1 2] = [3 12 6] |
Exponentiation of arrays | A .^ B | [8 5 21] .^ [2 3 1] = [64 125 21] |
Exponentiation involving a scalar and array | a .^ B B .^ a | 2 .^ [4 2 7] = [16 4 128] [3 10 -1] .^ 3 = [27 1000 -1] |
Order of Precedence
Like mathematics, MATLAB has an order of precedence. In general, it follows this order: parentheses, transpose/exponentiation, negation, multiplication/division, addition/subtraction, relational operators, and logical operators. See MATLAB’s documentation on order of precedence for more details.