2/08 lecture sketch note: contains items (marked with !) not covered in lecture announcements + hand in P2 into correct pile (by last name of first person listed) + bonus exercise E4 has been posted + P3 might not be posted until weekend + we will be switching to online submission of homework + you will get an e-mail (cs100M e-mail generally sent to your netid account) + if get it, can ignore until after prelim, but don't delete it! + if don't get it, send me (tyan@cs.cornell.edu) e-mail by end of weekend + E1-E3 will be available at 5pm Friday in Carpenter lab (and at review) + P1 will be available at 5pm Friday in Carpenter lab (and at review) + P2 will be available at 5pm Monday in Carpenter lab (by review session?) + P2 solution will soon be posted; read it! + bonus for scribing? reminders + learn actively: try out code from lecture and variations thereof + review session 3-5pm Sunday, Kimball B11 (? use door into building between Upson and Kimball) + prelim T1 tuesday evening (rooms announced/posted tuesday) + newsgroup is required; check at least every other day + bonus for online evaluations: lecture, project + bonus for asking questions in lecture: hand in full sheet of paper with: name, netid, cuid, question + netid ~= cuid (netid is like tky1, cuid is like 123456) topics + logical/boolean arrays/values + logical operations and functions + DeMorgan's law + uses of logical arrays: control, counting, indexing/filtering _______________________________________________________________________________ logical or boolean arrays or values: + *logical* or *boolean* means dealing with *true* or *false* values + a *logical* array is a special kind of array with true/false values + created by logical operations and logical functions + *canonical* means standard, usually special and unique, representation + canonical representation of *false* is 0 >> if 0 >> 'hi!' >> end + canonical representation of *true* is 1 >> if 1 >> 'bye!' >> end !+ note: $nan$ is a special value meaning ! "Not A Number" or "does not exist" or "makes no sense" !+ note: $nan$ is neither true nor false, but don't rely on that! !+ non-zero, non-nan values are often treated as *true* ! >> if 5 ! >> 'bye!' ! >> end _______________________________________________________________________________ logical operations and functions + comparison, *relational* operations: $<$ $<=$ $==$ $~=$ $>=$ $>$ + boolean connectives infix operators: $&$ $|$ prefix operator: $~$ prefix functions: $and$ $or$ $not$ $xor$ $or$ is inclusive or: "is one or both conditions true?" $xor$ is exclusive or: "is either but not both conditions true?" note: generally, operators are preferred to functions $0-1)$: either x<0 or x>-1 but not both note: if doesn't matter, "or" is usually preferred to "xor" $~(0=x | x >=5) & (10>=x | x>=15)$: (0>=x or x>=5) and (10>=x or x>=15) <--+ | note: last 2 examples are the same condition! <-----------------------------+ + demorgan's laws: not (i have fork or i have spoon) = (not i have fork) and (not i have spoon) not (i have fork and i have spoon) = (not i have fork) or (not i have spoon) + false is identity for or: false or C = C or false = C, for every condition C + true is identity for and: true and C = C and true = C, for every condition C + quantifiers: $any$ $all$ $any$: accumulation using or $all$: accumulation using and $any([])$: false (no example) since false is identity for or $all([])$: true (no counter-examples) since true is identity for and $all(rem(x,2) == 0)$: "all #s are even?" <-- opposite $any(rem(x,2) ~= 0)$: "any #s are odd?" <-- conditions + demorgan's again: $all(C)$ = $~any(~C)$: "all are true = not any false" $~all(~C)$ = $any(C)$: "not all false = some true" ! + tests: $isempty$ $isfinite$ $isinf$ $isnan$ ! ex: $isempty([])$: 1 (true) ! $isempty(5)$: 0 (false: 5 = [5] is a non-empty list of length 1) ! $isempty([0 0 0])$: 0 (false: [0 0 0] is a non-empty list of length 3) ! + *cast*ing means converting from one type to another ! + cast: $logical$ ! ! $logical([5 0 -3 28 0])$: true false true true false _______________________________________________________________________________ accumulation review (justification of $all([])$ = true) + for "nice" operations (e.g. addition, multiplication, min, max, or, and) if we split a list L into two sublists L1 and L2, then op(L) = op(op(L1), op(L2)) e.g. sum of entire list = sum of sum of sublists min of entire list = min of min of sublists and of entire list = and of and of sublists + sum([1 3 5 1]) = sum([1 3]) + sum([5 1]) = sum([1 1]) + sum([3 5]) = sum([1 3 5 1]) + sum([]) in general, sum(L) = sum(L) + sum([]) , so sum([]) = 0 + prod([1 3 5 1]) = prod([1 3]) * prod([5 1]) = prod([1 1]) * prod([3 5]) = prod([1 3 5 1]) * prod([]) in general, prod(L) = prod(L) * prod([]), so prod([]) = 1 + min([1 3 5 1]) = min( min([1 3]), min([5 1]) ) = min( min([1 1]), min([3 5]) ) = min( min([1 3 5 1]), min([]) ) in general, min(L) = min( min(L), min([]) ), so min([]) = $inf$ + max([1 3 5 1]) = max( max([1 3]), max([5 1]) ) = max( max([1 1]), max([3 5]) ) = max( max([1 3 5 1]), max([]) ) in general, max(L) = max( max(L), max([]) ), so max([]) = $-inf$ + any([1 3 5 1]) = any([1 3]) | any([5 1]) = any([1 1]) | any([3 5]) = any([1 3 5 1]) | any([]) in general, any(L) = any(L) | any([]) , so any([]) = 0 (false) + all([1 3 5 1]) = all([1 3]) & all([5 1]) = all([1 1]) & all([3 5]) = all([1 3 5 1]) & all([]) in general, all(L) = all(L) & all([]) , so all([]) = 1 (true) _______________________________________________________________________________ uses of logical arrays !+ control "flow" of program: ! + (scalar) test of a conditional ($if$) ! + (scalar) guard of a $while$ loop (seen in P1, covered soon) + counting ! $sum(x>0)$: number of positive elements ! $sum(x(1:end-1) == x(2:end))$: number of duplicate adjacent elements $sum(rem(x,2) == 0)$: number of even elements + $find$ computes position of true values ! $find(x<0)$: positions of negative elements $find(rem(x,2) == 0)$: positions of even elements ! $find([0 1 1 0 0 1])$: 2 3 6 ! $x(find(x<0))$: negative elements $x(find(rem(x,2) == 0))$: even elements ! $x(find([0 1 1 0 0 1]))$: x([2 3 6]) = [x(2) x(3) x(6)] + indexing: equivalent to positions of true elements, i.e. no need for $find$ $x(rem(x,2) == 0)$: even elements ! $x(x>0)$: positive elements ! $x(x>0) = []$: delete positive elements ! $x(logical([0 1 1 0 0 1]))$: x([2 3 6]) = [x(2) x(3) x(6)] ! $x([0 1 1 0 0 1])$: error! x(0) fall off left end of array! ! $str(str == ' ') = '*'$: replace each space in string $str$ with asterisk ! indexing with a logical array = *filtering* out certain elements _______________________________________________________________________________ answers to questions in lecture Q: how do you use exclusive or in matlab? A: $xor(x<0, x>-1)$ Q: can you give $xor$ more than 2 arguments? A: no. next lecture: i'll show you how to write function $myxor$ to take 3 arguments Q: how do you get number of trues, e.g. number of even numbers in a list? A: $sum(rem(x,2)==0)$ Q: why wouldn't that add the even numbers instead of counting? A1: remember, canonical true is 1, false is 0, so $sum(rem(x,2)==0)$ A1: adds a 1 for every true in the list $rem(x,2)==0$. A2: to extract out the even elements, use $x(rem(x,2)==)$. A3: to add all the even elements, use $sum( x(rem(x,2)==) )$. Q: is $00$, like in assignment statements, Q: where the variable is on the left? A1: careful! sounds like you are close to confusing assignment $=$ with A1: mathematical equality, which matlab represents as $==$. A2: all these are fine: $0x$, $x==y$, $x==0$, $0==x$, A2: $x+1x+1$ A3: if you are doing a series of comparisons, it often helps to orient A3: all comparisons in the same direction, e.g. $00 & x<1$. Q: what is difference between (inclusive) or and exclusive or? A: inclusive: 1 or both conditions must be true A: exclusive: exactly 1 of the two conditions must be true Q: what happens if you drop parentheses from $~(0=x, x>=5) & or(7>=x, x>=9)$ A: instead of $(0>=x | x>=5) & (7>=x | x>= 9)$