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: Addition
Example Math Expression: 3 + 3
MATLAB Command: 3 + 3
Operation: Subtraction
Example Math Expression: 3 – 3
MATLAB Command: 3 - 3
Operation: Negation
Example Math Expression: -1
MATLAB Command: -1
Operation: Multiplication
Example Math Expression: 3 * 3
MATLAB Command: 3 * 3
Operation: Division
Example Math Expression: 15 ÷ 3
MATLAB Command: 15 / 3
Operation: Exponentiation
Example Math Expression: 32
MATLAB Command: 3^2
OperationExample Math ExpressionMATLAB Command
Addition3 + 33 + 3
Subtraction3 – 33 - 3
Negation-1-1
Multiplication3 * 33 * 3
Division15 ÷ 315 / 3
Exponentiation323^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:

    \[   \left[ {\begin{array}{cc}     a & b \\     c & d \\   \end{array} } \right] x \left[ {\begin{array}{cc}     e & f \\     g & h \\ \end{array} } \right] = \left [ {\begin{array}{cc}     ae + bg & af + bh \\     ce + dg & cf + dh \\ \end{array} } \right] \]

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:

    \[   \left[ {\begin{array}{cc}     a & b \\     c & d \\   \end{array} } \right] x \left[ {\begin{array}{cc}     e & f \\     g & h \\ \end{array} } \right] = \left [ {\begin{array}{cc}     ae & bf \\     cg & dh \\ \end{array} } \right] \]

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: Addition
Element-wise Operator: +
Matrix Math Operator: +
Operation: Subtraction
Element-wise Operator: -
Matrix Math Operator: -
Operation: Negation
Element-wise Operator: -
Matrix Math Operator: -
Operation: Multiplication
Element-wise Operator: .*
Matrix Math Operator: *
Operation: Division
Element-wise Operator: ./
Matrix Math Operator: /
Operation: Exponentiation
Element-wise Operator: .^
Matrix Math Operator: ^
OperationElement-wise OperatorMatrix 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: Addition
Format: A + B
MATLAB Example(s): [1 0 5] + [1 3 2] = [2 3 7]
Element-wise Operation: Subtraction
Format: A – B
MATLAB Example(s): [1 0 5] - [1 3 2] = [0 -3 3]
Element-wise Operation: Multiplication of a scalar and an array
Format: a * B
MATLAB Example(s): 3 * [3 5 4] = [9 15 12]
Element-wise Operation: Multiplication of arrays
Format: A .* B
MATLAB Example(s): [8 1 5] .* [2 3 4] = [16 3 20]
Element-wise Operation: Division of an array by a scalar
Format: A / b
MATLAB Example(s): [16 10 22] / 2 = [8 5 11]
Element-wise Operation: Division of arrays
Format: A ./ B
MATLAB Example(s): [24 25 35] ./ [8 5 7] = [3 5 5]
Element-wise Operation: Division of a scalar by an array
Format: a ./ B
MATLAB Example(s): 12 ./ [4 1 2] = [3 12 6]
Element-wise Operation: Exponentiation of arrays
Format: A .^ B
MATLAB Example(s): [8 5 21] .^ [2 3 1] = [64 125 21]
Element-wise Operation: Exponentiation involving a
scalar and array
Format: a .^ B
B .^ a
MATLAB Example(s): 2 .^ [4 2 7] = [16 4 128]
[3 10 -1] .^ 3 = [27 1000 -1]
Element-wise OperationFormatMATLAB Example(s)
AdditionA + B[1 0 5] + [1 3 2] = [2 3 7]
SubtractionA – B[1 0 5] - [1 3 2] = [0 -3 3]
Multiplication of a scalar and an arraya * B3 * [3 5 4] = [9 15 12]
Multiplication of arraysA .* B[8 1 5] .* [2 3 4] = [16 3 20]
Division of an array by a scalarA / b[16 10 22] / 2 = [8 5 11]
Division of arraysA ./ B[24 25 35] ./ [8 5 7] = [3 5 5]
Division of a scalar by an arraya ./ B12 ./ [4 1 2] = [3 12 6]
Exponentiation of arraysA .^ 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]
The highlighted operations require a period character to properly perform the element-wise operation.

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.