Relational and Logical Operations in MATLAB

Leave a Comment
MATLAB: The Language of Technical Computing
MATLAB: The Language of Technical Computing

Welcome once again, to a new post on MATLAB!

Today as you might have read from the title, we are going to learn about Relational and Logical Operations that we can perform in MATLAB over arrays. Matrices can undergo relational and logical operations made up of GREATER/ LESSER THAN, (NOT) EQUAL TO, XOR, OR, AND, NOT operators in MATLAB. To learn that first, we must first learn about relational and logical operators in general.


Relational Operations

Relational operators do not manipulate operate upon matrices. Relational Operators work inside Relational Expressions. Relational Operators are used to form a corresponding Relational Expression over the matrices being operated. Those Relational Expressions result in a true or false result.

The operations such as greater than, lesser than, equal to and not equal to, are called as relational operators.

The following commands are used to implement Relational Operations in MATLAB
  • A > B - Greater than - Returns 1 if element of A is greater than B, else 0
  • A < B - Less than - Returns 1 if element of A is less than B, else 0
  • A == B - Equality - Returns 1 if element of A is equal to B, else 0
  • A ~= B - Inequality - Returns 1 if element of A is not equal to B, else 0
We can see the above operations performed as below.

Performing Relational Operations on matrices in MATLAB
Performing Relational Operations on matrices in MATLAB

Logical Operations

Logical Operations are used to derive a logical result out of two entities, in this case matrices. The expressions these operators are included in are called as logical expressions. This result is usually of the type of a logical true or logical false.

The different logical operators in MATLAB are XOR, OR, AND, NOT.

The following commands are used to implement Logical Operations in MATLAB
  • xor(A, B) - To find the XOR of elements of A and B
  • or(A, B) - To compute OR operation on elemex`nts of A and B
  • and(A, B) - To compute AND operation on elements of A and B
  • not(A) - To compute NOT of elements of A

Same as above, all of the operations we discussed above can be seen in the code snippet below.

Performing Logical Operations on matrices in MATLAB
Performing Logical Operations on matrices in MATLAB


Hence we finish out lesson on relational and logical operators in MATLAB. I really hope you found it just as useful as I did. Meet you in the next post. Stay happy :)



0 comments:

Post a Comment

Powered by Blogger.