Comparison operators and precedence rules
In the previous section, we discussed the basic arithmetic operations. In this section, we will learn how to compare different variables. Octave (like many other programming languages) uses very intuitive operator characters for this. They are:
|
Evaluates to true if |
|
Evaluates to true if |
|
Evaluates to true if |
|
Evaluates to true if |
|
Evaluates to true if |
|
Evaluates to true if |
For Octave's comparison operators, true is equivalent to a non-zero value and false is equivalent to 0. Let us see a few examples—recall the matrix A
from Command 93:
octave:108> A(2,1) == 1 ans = 1 octave:109>A(2,1) == 2 ans = 0 octave:110> A(2,1) > 0 ans = 1 octave:111> A(2,1) != 4 ans = 1
Instead of using !=
for "not equal to", you can use ~=
.
You may be familiar...