Relational expressions help you compare values to see if one is bigger, smaller, or equal to another. This is useful to make decisions in your program.
0
0
Relational expressions in MATLAB
Introduction
Checking if a number is greater than a limit before proceeding.
Comparing two variables to see if they are equal.
Finding out if a value is less than or equal to a threshold.
Filtering data by selecting only values that meet a condition.
Controlling loops that run while a condition is true.
Syntax
MATLAB
result = (value1 operator value2); % Operators: % == Equal to % ~= Not equal to % > Greater than % < Less than % >= Greater than or equal to % <= Less than or equal to
The result is always logical: true (1) or false (0).
Use parentheses to make the comparison clear and avoid mistakes.
Examples
This checks if
a is less than b. The result will be true because 5 is less than 10.MATLAB
a = 5; b = 10; result = (a < b);
This checks if
x is equal to y. The result will be true because both are 7.MATLAB
x = 7; y = 7; result = (x == y);
This checks if
score is greater than or equal to pass_mark. The result will be true because 85 is more than 60.MATLAB
score = 85; pass_mark = 60; is_pass = (score >= pass_mark);
Sample Program
This program compares two numbers a and b. It prints a message if a is less than b. Then it checks if they are equal and shows the result.
MATLAB
a = 12; b = 20; if a < b disp('a is less than b'); else disp('a is not less than b'); end is_equal = (a == b); disp(['Are a and b equal? ', mat2str(is_equal)]);
OutputSuccess
Important Notes
Relational expressions return logical values: 1 for true and 0 for false.
You can use relational expressions inside if statements to control program flow.
Remember to use == for equality, not = which is for assignment.
Summary
Relational expressions compare two values and return true or false.
Common operators include ==, ~=, >, <, >=, and <=.
They are useful for making decisions and controlling program flow.