Matlab Relational Logical Operators
Matlab Relational Logical Operators
Table of Contents
Relational Operators................................................................................................................................................ 1
Scaler to Scaler................................................................................................................................................... 1
Scaler to Vector/Matrix........................................................................................................................................ 2
Row Vector to Row Vector..................................................................................................................................3
Column Vector to Column Vector....................................................................................................................... 3
Row Vector to Column Vector.............................................................................................................................4
Column Vector to Row Vector.............................................................................................................................4
Row Vector to Matrix...........................................................................................................................................5
Column Vector to Matrix......................................................................................................................................5
Matrix to Matrix.................................................................................................................................................... 6
Logical Operators.....................................................................................................................................................7
Relational Operators
Relational Operators are used to compare the scalers, vectors, Matrices and Strings.
Scaler to Scaler
Output is always logical 0 or 1 depending on the applied condition is flase or true.
Example:
1
x=5,y=10,
x = 5
y = 10
z1=(x==y), z2=(x<y),z3=(x>y),z4=(x<=y),z5=(x>=y)
z1 = logical
0
z2 = logical
1
z3 = logical
0
z4 = logical
1
z5 = logical
0
Scaler to Vector/Matrix
Each element of Vector/Matrix is compared with the Scaler. As a result you get a Vector/Matrix of logical 0
and 1.
% When one operand is scaler and other one is vector or Matrix: Scaler to Vector/Matrix
x=5, u=[ 1 2 3 5 10 12] % x is scaler and u is row vector
x = 5
u = 1×6
1 2 3 5 10 12
z1=(x>=u), z2=(x~=u)
x = 10
A = 3×3
11 10 13
15 4 6
4 18 20
z2=(x==A),z3=(x>A)
2
1 0 0
As a result you get the a row vector having logical 0 and 1 elements
% When both operands are row vector: Row Vector to Row Vector
u_1=[15 17 13 18], u_2=[ 10 14 16 19]
u_1 = 1×4
15 17 13 18
u_2 = 1×4
10 14 16 19
z1=(u_1>u_2), z2=(u_1~=u_2)
As a result you get the a column vector having logical 0 and 1 elements
% When both operands are column vector: Column Vector to Column Vector
v_1= [1;2;3; 16],v_2=[10; 1; 17; 23]
v_1 = 4×1
1
2
3
16
v_2 = 4×1
10
1
17
23
z1=(v_1<=v_2), z2=(v_1==v_2)
3
0
0
% When one operand is row vector and other is column vector: Row Vector to Column Vector
u=[1 4 5 6], v=[2; 5; 7]
u = 1×4
1 4 5 6
v = 3×1
2
5
7
z1=(u<=v), z2=(u>v)
% When one operand is column vector and other is row vector: Column Vector to Row Vector
v=[23; 34; 46; 11], u=[22 45 57]
v = 4×1
23
34
46
11
u = 1×3
22 45 57
z1=(v>=u), z2=(v~=u)
4
z2 = 4×3 logical array
1 1 1
1 1 1
1 1 1
1 1 1
% When one operand is row vector and other is matrix: Row Vector to Matrix
u=[11 17 20 23], A=[11 14 12 10; 14 16 34 23]
u = 1×4
11 17 20 23
A = 2×4
11 14 12 10
14 16 34 23
z1=(u<A), z2=(u>=A)
% When one operand is column vector and other is matrix: Column Vector to Matrix
v=[22; 34; 11;],A=[11 35; 25 22; 45 10]
v = 3×1
22
34
11
A = 3×2
11 35
25 22
45 10
z1=(v>=A),z2=(v<A)
5
z1 = 3×2 logical array
1 0
1 1
0 1
z2 = 3×2 logical array
0 1
0 0
1 0
Matrix to Matrix
Condition: dimension of both matrices should be same.
A = 3×3
21 2 3
4 7 9
11 3 45
B = 3×3
11 34 23
34 67 11
23 34 67
z1=(A>=B),z2=(A==B)
Important Points
• Be careful while using "==" operator. There is clear difference between "=" and "==".
• = refer to assign the value in new variable
• == refer to compare the operands
• ‘==’ operator gives logical output 1 when both the operands are exactly same. So one should be very
careful while using operator.
• In case of Complex number, the operators >, <, >=, and <= use only the real part of the operands
in performing comparisons, while operators == and ~= test both real and imaginary parts of the
operands.
6
x1 = 4
x2 = 4.0000
% check abs(x1-x2)<= accuracy limit rather than x1==x2 (Wein's law varification)
z1=(x1==x2),z2=abs(x2-x1)<=10^-3 % See the difference in output
z1 = logical
0
z2 = logical
1
x=sin(2*pi),x==0
x = -2.4493e-16
ans = logical
0
z3 = 10.0000 + 10.0000i
z4 = 10.0000 + 50.0000i
ans = logical
1
ans = logical
0
Logical Operators
7
Refer input 1 = TRUE, 0 = False
For purpose of logical operations, Matlab treats an operand as true (logical 1), if it is non-zero value, and
false if it is zero (logical 0).
x = 1×4
0 5 3 7
y = 1×4
0 -2 8 7
m=(x>y)&(x>4)
n=x|y
z=~(x|y)
p=xor(x,y)
x = 1×11
1 2 5 7 90 34 10 11 15 19 20
% find out all the elements of vector x having values more than 2 and less than 15
x((x>2)&(x<15))
8
ans = 1×4
5 7 10 11
A = 3×3
1 2 3
23 45 12
21 4 14
ans = 2×1
12
14
% find out all the elements of matrix A having value more than 4 and less than or equal to 14
A((A>4)&(A<=14))