MATLAB:Logical Operators

From PrattWiki
Jump to: navigation, search

Logical operators in MATLAB are those that link logical statements together and return true (1) or false (0) in a logical matrix depending upon the nature of the logical operator and the value of the components. Many times, logical operators are used to link together the results of several relational operators.

Logical Operators

There are four main logical operators in MATLAB:

Name Symbol Example with symbol Example in code Text description
NOT ~ ~A not(A) True if A is false
AND & A & B and(A, B) True if both A and B are true
OR | A | B or(A, B) True if either A or B (or both!) are true
XOR xor(A, B) True if either A or B (but NOT both!) are true

The following table shows the results of four different pairs of input values to the logical operators above:

A B ~A ~B A & B A | B xor(A, B)
0 0 1 1 0 0 0
0 1 1 0 0 1 1
1 0 0 1 0 1 1
1 1 0 0 1 1 0

Common Errors

The most common errors with logical operators involve the order of operations. In the absence of parenthesis, any NOT operations go first, followed by AND, then finally OR.


Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links