02/06 announcements + hand in E3 + AEW still open until 1pm thursday + new sections Sec 15 #647222 T 1220pm UP 205 Sec 16 #658508 W 1220pm PH 403 reminders: bonus: online evals, Qs in lec (write name, cuid, netid, Q) topics (vector concat, conditionals) + E3 (for notes and much more: see solutions to E3) + Pascal's triangle + subarrays (indexing) _______________________________________________________________________________ print rows 0..N of pascal's triangle notes: + by "row", we mean "the non-zero elements of a row" + print 1+N rows: 0, 1, 2, ..., N + for now, without nice formatting (bacteria: also no formatting!) + pay attention to vector concat triangle matlab representation of row i 0 0 1 0 0 [ 1 ] 0 \ / \ / \ / \ / |\ 0 1 1 0 [ 1 1 ] 1 / \ / \ / \ / \ |\|\ 0 1 2 1 0 [ 1 2 1 ] 2 \ / \ / \ / \ / |\|\|\ 1 3 3 1 [ 1 3 3 1 ] 3 / \ / \ / \ / \ |\|\|\|\ 1 4 6 4 1 [ 1 4 6 4 1 ] 4 solution 1: N = 4; row = [ 1 ]; disp(row); for i = 1:N new = zeros(1,i+1); for j = 1:i new(j) = new(j) + row(j); new(j+1) = new(j+1) + row(j); end row = new; disp(row); end observation: [ 1 3 3 1 ] makes 2 copies: [ 1 3 3 1 ] and [ 1 3 3 1 ] |\|\|\|\ | | | | \ \ \ \ 1 3 3 1 1 3 3 1 so the result is the sum [ 1 3 3 1 ] |\|\|\|\ 1 3 3 1 * = [ row 0 ] * 1 3 3 1 = [ 0 row ] [ 1 4 6 4 1 ] solution 2: N = 4; row = [ 1 ]; disp(row); for i = 1:N row = [row 0] + [0 row]; disp(row); end note similarities with P2.4 (bacteria): + both take an old row/population and produce a new row/population + both process each item twice (add left + add right versus age + replicate) + both can be solved with nested loops or with just one loop (might also need conditionals -- only mature bacteria replicate) _______________________________________________________________________________ subarrays + we have already seen $x(i)$ for $i$ an integer 1..$length(x)$ means "i-th element of x" + Matlab uses 1-relative indexing 1 2 3 +---+---+---+ | 7 | 8 | 5 | +---+---+---+ Java uses 0-relative indexing 0 1 2 3 + matlab allows one to access *subarrays*: multiple elements from an array + ex: every other element of x (1st, 3rd, 5th, ...): $x(1:2:end)$ + ex: 5th, 1st, 1st, 8th: $x([5 1 1 8])$ + ex: first five elements: $x(1:5)$ + ex: in reverse order: $x(end:-1:1)$ two different behaviors + read: positions must be in range 1..length(x) + write: positions >= 1; if fall off right end, array is auto-extended with 0s review: update rule for $lhs = rhs$ + evaluate $lhs$ to get locations + evaluate $rhs$ to get values + declare $lhs$ if non-existent + put values into locations (cross out old values if any) question: why does order of 2nd and 3rd step matter? + start from scratch: $x(5) = x(5)+1$ assignment: + generally, size of $lhs$ must match size of $rhs$ ok: x([1 2]) = x([11 10]) (assuming length(x)>=11) bad: x([1 2 3]) = x([11 10]) + exception 1: auto-replicate scalar $rhs$ ok: x([1 2]) = x(11) + exception 2: assigning $[]$ deletes ok: x([1 2]) = [] + question: have deletion, how do insertion? answer: use concat ex: insert 3 between x(5), x(6): x = [x(1:5) 3 x(6:end)] _______________________________________________________________________________ questions in lecture: Q: can you access matrix elements in any order? A: yes. Q: in E3.3 solution, is $median(x)$ instead of $sum(x)-max(x)-min(x)$ ok? A: yes. Q: can you insert a new spot into an array without giving it a value? A1: no, you need to provide a value, e.g. 0. A2: depends on what you meant by "value" -- $nan$ (Not A Number) can be used. Q: (re: solution-2 for pascal's triangle) Q: why isn't loop variable $i$ used inside the loop? is that ok? A1: the loop is just to repeat something $N$ times. A2: as seen in E2.3, it is ok to not use the loop variable in the loop body. Q: can you use $fliplr([row 0])$ instead of $[0 row]$? A: yes, but it is faster and simpler to use $[0 row]$.