Skip to main content
Logo image

Section 1.4 Arithmetic Operators with Scalars

MATLAB can be used directly as a calculator. After all, a number can simply be viewed as a scalar and as such is a single-element 1D matrix.

Subsection 1.4.1 Scalar Operators

Table 1.11. Arithmetic Operators
Operator Symbol Example
Addition + 5 + 3
Subtraction - 5 - 3
Multiplication * 5 * 3
Division / 5 / 3
Left Division \ 5 \ 3 (= 3 / 5)
Exponentiation ^ 5^3 (=5\(^3\) = 125)
I know. Left division seems like pretty much the most useless thing ever. We’ll have to wait for matrices with more than one element for this to make any kind of sense.
Table 1.12. Operator(s)
Operator Symbol Precedence
Parentheses () highest
Exponentiation ^      |
Multiplication and Division * / \      |
Addition and Subtraction + - lowest

Subsection 1.4.2 Examples

  • In the command window, enter the expression at the prompt and the result is echoed back to you.
  • The result of the current expression is stored in a predefined variable ans.
  • ans can be used as a normal variable (but remember that as soon as you perform a new operation, the value of ans reflects the result of this most recent computation and no longer that of the previous operation.
  • Adding a semicolon ; to the end of the command line suppresses the output, while typing the variable’s name displays its value.
Simple example:
>> 7 + 8/2
ans =
    11
>> ans*3
ans =
    33
>> ans/3;
>> ans
ans =
    11
Example with operator precedence:
>> (7 + 8)/2
ans =
    7.5000
>> 4 + 5/3 + 2
ans =
    7.6667
>> 27^(1/3) + 32^0.2
ans =
    5

Activity 1.2.

What is the result of the following evaluation in MATLAB:
>> 27^1/3 + 32^0.2