Boolean and relational operators in Matlab

  • Published on
    14-Jun-2015

  • View
    192

  • Download
    0

Embed Size (px)

DESCRIPTION

For more information, visit: http://matrixlab-examples.com/boolean-operator.html AND, OR, NOT, XOR Boolean and relational operators are explained so as to be used in Matlab, Freemat and other programming languages. Specific examples are given.

Transcript

  • 1. Boolean OperatorsIn Matlab

2. Boolean OperatorsYou use logical operators in conditionalexpressions much as you use mathoperators in numeric expressions.SYMBOLin MatlabMEANING& logical AND operator| logical OR operator~ logical NOT (complement)xor exclusive-OR 3. Boolean OperatorsThe & (AND) logical operatorThe & operator lets you specify multiple conditions that must be true beforean action can be taken.Condition 1 Condition 2 Result ofCondition 1 & Condition 20 0 00 1 01 0 01 1 1 4. Boolean OperatorsThe & (AND) logical operatorExample in Matlab:a = [0 0 1 1];b = [0 1 0 1];c = a & bProduces:c = [0 0 0 1] 5. Boolean OperatorsThe | (OR) logical operatorThe | operator lets you create a more flexible set of conditions that must bemet before an action can take place.Condition 1 Condition 2 Result ofCondition 1 | Condition 20 0 00 1 11 0 11 1 1 6. Boolean OperatorsThe | (OR) logical operatorExample in Matlab:a = [0 0 1 1];b = [0 1 0 1];c = a | bProduces:c = [0 1 1 1] 7. Boolean OperatorsThe ~ (NOT) logical operatorThe ~ operator lets you negate a condition: if a condition is false, the NOToperator makes the condition true; if a condition is true, NOT makes itfalse.Condition Result of~ Condition0 11 0 8. Boolean OperatorsThe ~ (NOT) logical operatorExample in Matlab:a = [0 1];b = ~aProduces:b = [1 0] 9. Boolean OperatorsThe xor (logical exclusive-OR)C = xor(A, B) performs an exclusive-OR operation on the correspondingelements of arrays A and B.A B Result ofxor(A, B)0 0 00 1 11 0 11 1 0 10. Boolean OperatorsThe xor logical operatorExample in Matlab:a = [0 0 1 1];b = [0 1 0 1];c = xor(a, b)Produces:c = [0 1 1 0] 11. Relational OperatorsThere are six relational operators in Matlab:Symbol Meaning< less than lreater than>= greater than or equal== equal~= not equal 12. Relational OperatorsThese operations result in a vector or matrix of the same size as theoperands, with 1 where the relation is true and 0 where its false.Example:Ifx = [2 5 3 9]y = [0 2 8 9]thenIf we type Well geta = x < y a = [0 0 1 0]a = x y a = [1 1 0 0]a = x >= y a = [1 1 0 1] 13. Relational and BooleanOperatorsNaturally, we can combine relational and boolean operators to createsophisticated manipulations.For example, if we have vector x = -5 : 10 and we want to find all theelements that are only within the interval -3 < x 2, we can find them likethis:y = x(-3 < x & x