%--------------------------------------------- % % For A321 course % % Started: Mon 16 Feb 2009, 02:28 PM % Last Modified: Mon 16 Feb 2009, 03:34 PM %--------------------------------------------- % % % Matlab can combine or negate relational expressions giving % true/false values using the standard logical Boolean operators % and, not, or. Those operators have the following symbols: % % ---------------- ------ % Boolean operator symbol % ---------------- ------ % and & % or | % not ~ % % Those follow the usual Boolean rules: % %-------------------------- % true & true = true % false & true = false % true & false = false % false & false = false % % true | true = true % false | true = true % true | false = true % false | false = false % % ~ true = false % ~ false = true %-------------------------- % %--------------------------------------------------------------------- echo on; %------------------------------------------------------------------ % Notice that "true" is represented by (almost) any nonzero value, % not just by 1. What happens if some component of a is set to NaN? %------------------------------------------------------------------ a = -3:5 not_a = ~a %--------------------------------------------------------------------- % Since true and false are represented in Matlab by 1 and 0, it can % be confusing at times whether an integer/double or boolean is meant. % I recommend you initialize any logical/Boolean variable to be % true or false rather than 1 and 0. Viz., %--------------------------------------------------------------------- b = true; %---------------------------------------------------------- % See what happens to the type of variable for a, not_a, b. %---------------------------------------------------------- whos %----------------------------------------------------------- % Warning: there are also doubled up logical operators, e.g. %----------------------------------------------------------- disp('Using && on the first element of b and not_a gives '); c(1) = not_a(1) && b(1) %-------------------------------------------------------------- % Those are for bit-by-bit negation, not at the level of bytes. % Try to run the && statement above for an entire vectors. % Just remove the (1) wherever it appears. %-------------------------------------------------------------- disp('Hit return to see effect of using && on the the entire vectors b and not_a '); pause; c = not_a && b echo off;