Relational & Logical Operators
Comparisons and logical tests produce values of class logical: 1 for true and 0 for false. They drive if statements and loops, and, as you'll see later, they can also select elements from arrays.
Relational operators
| Operator | Meaning |
|---|---|
== |
Equal to |
~= |
Not equal to |
<, <= |
Less than, less than or equal |
>, >= |
Greater than, greater than or equal |
a = 7;
b = 10;
disp(a == b)
disp(a ~= b)
disp(a < b)
disp(a >= 7)
result = (a > 5)
class(result)0
1
1
1
result = 1
ans = logical
Note: "Not equal" is
~=in MATLAB. Octave also accepts!=, but MATLAB does not, so always write~=.
Comparing arrays
Relational operators work element by element on arrays and return a logical array of the same size:
temps = [12 25 31 18 29];
hot = temps > 24
disp(sum(hot)) % true counts as 1
disp(temps(hot)) % keep only the hot dayshot =
0 1 1 0 1
3
25 31 29
Logical operators
| Operator | Meaning | Works on |
|---|---|---|
& |
AND | Arrays, element-wise |
| |
OR | Arrays, element-wise |
~ |
NOT | Arrays, element-wise |
xor(a, b) |
Exclusive OR | Arrays, element-wise |
&& |
AND with short-circuit | Scalars only |
|| |
OR with short-circuit | Scalars only |
x = [1 0 1 0];
y = [1 1 0 0];
disp(x & y)
disp(x | y)
disp(~x)
disp(xor(x, y))
disp(any(x))
disp(all(x)) 1 0 0 0
1 1 1 0
0 1 0 1
0 1 1 0
1
0
any is true when at least one element is nonzero, all when every element is. Any nonzero number counts as true: ~5 is 0 and ~0 is 1.
Short-circuit operators
&& and || evaluate the right side only if needed. With &&, if the left side is false the result must be false, so the right side is skipped. This lets you guard a test that would otherwise fail:
data = [];
if ~isempty(data) && data(1) > 0
disp('first value is positive')
else
disp('no data or first value not positive')
end
count = 0;
total = 10;
if count ~= 0 && total / count > 2
disp('average above 2')
else
disp('nothing to average')
endno data or first value not positive
nothing to average
With a plain &, both sides would be evaluated and data(1) would raise an "index out of bounds" error on the empty array. Use &&/|| in if and while conditions with scalar tests, and &/| when combining arrays.
Precedence and ranges
Arithmetic happens before comparisons, which happen before &, which happens before |. A common mistake is writing a range test the way you would in maths:
x = 15;
disp(0 < x < 10) % wrong: (0 < x) is 1, and 1 < 10 is true
disp(0 < x && x < 10) % right1
0
0 < x < 10 is evaluated as (0 < x) < 10, comparing a logical 1 with 10, so it is always true for positive x. Split range checks into two comparisons joined with && (or & for arrays).
Comparing text and floating point
== compares char arrays character by character and needs equal lengths; use strcmp to compare whole texts. For floating point, compare with a tolerance:
disp(strcmp('apple', 'apple'))
disp(strcmp('apple', 'apples'))
disp('cat' == 'cut')
disp(abs(0.1 * 3 - 0.3) < 1e-10)1
0
1 0 1
1
Test yourself
Which operator means "not equal" in MATLAB?
!=<>~==/=
What is the result of [3 8 1] > 2?
1[1 1 0][0 1 1][3 8]
Why prefer && over & in if ~isempty(v) && v(1) > 0?
&&is faster for arrays&&skipsv(1) > 0whenvis empty, avoiding an index error&does not exist- They are identical