Matlab - Boolean and Relational Operators
MatrixLab Examples
For detailed codes and more examples in Matlab, visit: https://matrixlab-examples.com/boolean-operator.html
Logical and: https://www.matrixlab-examples.com/logical-and.html
Logical or: https://www.matrixlab-examples.com/logical-or.html
Logical xor: https://www.matrixlab-examples.com/xor.html
Logical not: https://www.matrixlab-examples.com/logical-not.html
Combinational logic: https://www.matrixlab-examples.com/combinational-logic.html
On this video I’m gonna briefly introduce you to boolean operators and basic relational operations in Matlab.
You use logical operators in conditional expressions very much as you use math operators in numeric expressions.
In Matlab, an & represents the AND operator, a | represents the OR operator, a ~ is for NOT and the function xor is used for the exclusive-or operation.
The AND operator lets you specify multiple conditions that must be true before an action can be taken. The result is true only if both conditions are true, otherwise the result is false.
This is an example of the logical AND. You can see here the inputs and outputs of the truth table.
The OR operator lets you create a more flexible set of conditions that must be met before an action can take place. The result is false only if both conditions are false, otherwise the result for an OR is true.
This is an example of the logical OR in Matlab. You can see here the inputs and outputs of the truth table.
The NOT operator lets you negate a condition: if a condition is false, the NOT operator makes the condition true; if a condition is true, NOT makes it false.
This is an example of the logical NOT. You can see here the input and output of the truth table.
Function xor performs an exclusive-OR operation on the corresponding elements of arrays A and B. The result is true only if either A or B are true.
Here’s an example of the exclusive-or. You can see the inputs and outputs of the truth table.
Regarding the relational operators, there are six of them in Matlab: less than, less than or equal, greater than, greater than or equal, equal and not equal.
These operations result in a vector or matrix of the same size as the operands, with 1 where the relation is true and 0 where it’s false.
Naturally, you can combine relational and boolean operators to create sophisticated manipulations. Here’s a way to extract values from a vector that fall within a specific interval.
6318117 Bytes