Objective
To explore and understand the use of relational operators in MATLAB for comparing data and
evaluating conditions.
Theory
Relational operators are used to compare values or arrays in MATLAB. They return a logical array of
the same dimensions, where each element indicates the result of the comparison. The primary
relational operators in MATLAB are:
Operator Description
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
~= Not equal to
Logical results:
1 (true) indicates the condition is satisfied.
0 (false) indicates the condition is not satisfied.
Materials Required
MATLAB Software
A computer system with MATLAB installed
Procedure
1. Set Up Data: Define two arrays for comparison.
A = [3, 5, 7, 9];
B = [4, 5, 6, 7];
2. Use Relational Operators: Perform operations between corresponding elements of A and B:
o Greater than:
result_gt = A > B;
o Less than:
result_lt = A < B;
o Greater than or equal to:
result_ge = A >= B;
o Less than or equal to:
result_le = A <= B;
o Equal to:
result_eq = A == B;
o Not equal to:
result_neq = A ~= B;
3. Display Results: Output the results of each operation.
disp('Greater than:'); disp(result_gt);
disp('Less than:'); disp(result_lt);
disp('Greater than or equal to:'); disp(result_ge);
disp('Less than or equal to:'); disp(result_le);
disp('Equal to:'); disp(result_eq);
disp('Not equal to:'); disp(result_neq);
4. Experiment with Different Data Types: Repeat the operations with matrices or mixed data
types (if applicable).
Observations
Operator Result Example with A = [3, 5, 7, 9] and B = [4, 5, 6, 7]
> Greater [0, 0, 1, 1] A > B
< Less [1, 0, 0, 0] A < B
>= Greater= [0, 1, 1, 1] A >= B
<= Less= [1, 1, 0, 0] A <= B
== Equal [0, 1, 0, 0] A == B
~= Not= [1, 0, 1, 1] A ~= B
Conclusion
Relational operators in MATLAB are effective for comparing data in arrays. They return logical values
indicating the comparison results, which are essential for conditional operations and decision-making
in programming.
Code
% Define arrays
A = [3, 5, 7, 9];
B = [4, 5, 6, 7];
% Relational operations
result_gt = A > B;
result_lt = A < B;
result_ge = A >= B;
result_le = A <= B;
result_eq = A == B;
result_neq = A ~= B;
% Display results
disp('Greater than:'); disp(result_gt);
disp('Less than:'); disp(result_lt);
disp('Greater than or equal to:'); disp(result_ge);
disp('Less than or equal to:'); disp(result_le);
disp('Equal to:'); disp(result_eq);
disp('Not equal to:'); disp(result_neq);