Skip to main content
Contents Index
Dark Mode Prev Up Next
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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.
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: