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.
Relational expressions in 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.
a is less than b. The result will be true because 5 is less than 10.a = 5; b = 10; result = (a < b);
x is equal to y. The result will be true because both are 7.x = 7; y = 7; result = (x == y);
score is greater than or equal to pass_mark. The result will be true because 85 is more than 60.score = 85; pass_mark = 60; is_pass = (score >= pass_mark);
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.
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)]);
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.
Relational expressions compare two values and return true or false.
Common operators include ==, ~=, >, <, >=, and <=.
They are useful for making decisions and controlling program flow.
Practice
Solution
Step 1: Understand the meaning of relational operators
The operator==checks equality,~=checks inequality, and>=,<=check greater or equal and less or equal respectively.Step 2: Identify the operator for NOT equal
The operator~=means NOT equal in MATLAB.Final Answer:
~= -> Option DQuick Check:
NOT equal operator = ~= [OK]
- Confusing == with ~=
- Using = instead of ==
- Mixing up >= and >
a is greater than or equal to 10 in MATLAB?Solution
Step 1: Recall MATLAB relational operator syntax
MATLAB uses >= to mean greater than or equal to.Step 2: Check each option for correct syntax
a >= 10 uses >= correctly. Options A, B, and C use invalid or reversed operators.Final Answer:
a >= 10 -> Option BQuick Check:
Greater or equal operator = >= [OK]
- Writing => instead of >=
- Writing =< instead of <=
- Adding extra = signs
a = 5; b = 7;
result = (a < b) & (b <= 7);
disp(result);
Solution
Step 1: Evaluate each relational expression
a < b is 5 < 7 which is true (1). b <= 7 is 7 <= 7 which is true (1).Step 2: Apply logical AND operator &
Both expressions are true, so (1 & 1) equals 1 (true).Final Answer:
1 -> Option AQuick Check:
True & True = 1 [OK]
- Confusing & with &&
- Expecting output as logical word instead of 1
- Misreading <= operator
x = 10;
if x = 10
disp('Equal to 10');
end
Solution
Step 1: Check the if condition syntax
In MATLAB, = is assignment, == is comparison. Using = in if causes error.Step 2: Confirm other parts are correct
Semicolon after disp is optional, end is correctly used, x is initialized.Final Answer:
Using = instead of == in if condition -> Option AQuick Check:
Comparison needs ==, not = [OK]
- Using = instead of ==
- Thinking semicolon is mandatory after disp
- Misplacing end keyword
v = [3, 6, 9, 12], which MATLAB expression returns a logical array indicating elements greater than 5 and less than or equal to 10?Solution
Step 1: Understand vector relational operations
Relational operators applied to vectors return logical arrays element-wise.Step 2: Combine conditions correctly with parentheses and &
Use parentheses around each condition: (v > 5) & (v <= 10) to get elements satisfying both.Step 3: Check other options for syntax errors
v > 5 && v <= 10 uses short-circuit &&, invalid for vectors. v > (5 & v) <= 10 is invalid syntax. (v > 5) | (v <= 10) uses OR instead of AND.Final Answer:
(v > 5) & (v <= 10) -> Option CQuick Check:
Use parentheses with & for combined conditions [OK]
- Using && instead of & for array operations
- Using | instead of & for AND
- Writing invalid combined expressions
Ready to prove it?
Pass the test and this pattern moves unknown
