3/15 prelim T2 reminders: + please try to leave the ends of every 3rd row empty + room assignments by last name are on the web (different from T1): Olin Hall 155: K - Z Olin Hall 255: A - J topics + T2 format + R2.3 tracing solution + E11 solution + 4 versions --e11v1, e11v2, e11v3, e11v4-- posted (soon) to Exercises page + useful trick: "coordinate matrices" and logical arrays + $repmat$ ! + pros and cons of "useful trick" =============================================================================== below are the ideas needed to solve E11 + the notes below are based on a newsgroup post and the material presented in lecture + the code (4 versions) are on the Exercises page; please look at all 4 versions hint -1: we reminded you of the (in)equation for a circle/disk. hint 0: maybe try it first for some specific, small values of $m$. + "try" = do it by hand + explicitly list the coordinates you "care about" to look for a pattern + two approaches to produce positions $(y,x)$ we "care about": I. find a way to generate $x$s from $y$s or a way to generate $y$s from $x$s to get all positions $(y,x)$ of interest. II. find a condition on $y$ and $x$ that characterizes ALL and ONLY positions $(y,x)$ of interest. hint 1: what is the equation (equality) for a line? ! + answer: Ax + By + C = 0 (for suitable constants A, B, C) ! + special case: y = mx + b (even more special: y = b) ! + special case: x = my + b (even more special: x = b) hint 2: what is the inequation (inequality) for a half-plane (points below a line)? ! + answer: Ax + By + C <= 0 (for suitable constants A, B, C) ! or: Ax + By + C >= 0 (for suitable constants A, B, C) ! ! + note: use $<$ instead of $<=$ ! or $>$ instead of $>=$ to exclude the line itself _______________________________________________________________________________ $*$s on the left, right, top, and bottom edges look at small example $e11(10)$: n = 20 11111111112 12345678901234567890 1 ************+++++*** top: (1,1), (1,2), ..., (1,19), (1,20) 2 *\ooooooooo++@@@++o* pattern: y = 1, x = 1:20 = 1:n 3 *-\ooooooo++@@@@@++* equation/condition: y == 1 4 *--\oooooo+@@@@@@@+* 5 *---\ooooo+@@@c@@@+* 6 *----\oooo+@@@@@@@+* 7 *-----\ooo++@@@@@++* 8 *------\ooo++@@@++o* 9 *-------\ooo+++++oo* m = 10 ******************** bottom: (10,1), (10,2), ..., (10,19), (10,20) pattern: y = 10 = m, x = 1:20 = 1:n equation/condition: y == m left: right: (1,1) (1,20) (2,1) (2,20) . . . . . . (9,1) (9,20) (10,1) (10,20) pattern: pattern: x = 1 x = 20 = n y = 1:10 = 1:m y = 1:10 = 1:m equation/condition: equation/condition: x == 1 x == n _______________________________________________________________________________ $\$s on the diagonal look at small example $e11(10)$: n 11111111112 12345678901234567890 list of positions we care about: 1 ************+++++*** (1,1) on edge -- can include or exclude 2 *\ooooooooo++@@@++o* (2,2) 3 *-\ooooooo++@@@@@++* (3,3) 4 *--\oooooo+@@@@@@@+* (4,4) 5 *---\ooooo+@@@c@@@+* (5,5) 6 *----\oooo+@@@@@@@+* (6,6) 7 *-----\ooo++@@@@@++* (7,7) 8 *------\ooo++@@@++o* (8,8) 9 *-------\ooo+++++oo* (9,9) m 10 ******************** (10,10) on edge -- can include or exclude pattern: y = x from 1 to 10 = m equation/condition: y == x _______________________________________________________________________________ $-$s below the diagonal look at small example $e11(10)$: n 11111111112 12345678901234567890 list of positions we care about: 1 ************+++++*** 2 *\ooooooooo++@@@++o* 2,1 3 *-\ooooooo++@@@@@++* 3,1 3,2 4 *--\oooooo+@@@@@@@+* 4,1 4,2 4,3 5 *---\ooooo+@@@c@@@+* 5,1 5,2 5,3 5,4 6 *----\oooo+@@@@@@@+* 6,1 6,2 6,3 6,4 6,5 7 *-----\ooo++@@@@@++* 7,1 7,2 7,3 7,4 7,5 7,6 8 *------\ooo++@@@++o* 8,1 8,2 8,3 8,4 8,5 8,6 8,7 9 *-------\ooo+++++oo* 9,1 9,2 9,3 9,4 9,5 9,6 9,7 9,8 m 10 ******************** pattern: equation/condition: given y, x = 1:y-1 x < y or given x, y = x+1:9 = x+1:m-1 y > x _______________________________________________________________________________ disk: we gave you the conditions + interior: d < r \ where d = sqrt((x-xc)^2 + (y-yc)^2) + boundary: r - .5 < d < r + .5 / or: abs (d - r) < .5 + note: if we had $r - .5 <= d <= r + .5$, this is the same as $abs(d-r) <= .5$ and $round(d) == r$, but with strict inequality, $round$ includes extra points when $d-r == .5$ _______________________________________________________________________________ useful trick: "coordinate matrices" and logical arrays in all the cases above, one approach is for each position in the matrix test a condition to see if we care about it if so, do something with the element at that position observe how this has very regular, very uniform pattern: even using a loop is kind of redundant! matlab allows us to avoid using loops in all the cases above. trick: + use $repmat$ to compute two matrices $x$, $y$ of the coordinates of all positions in the matrix + use $x$ and $y$ compute a logical array indicating which positions we care about + use the logical array to index into the matrix example: positions of a 3-by-4 matrix $s$: y coordinates of each position: s 1 2 3 n=4 y 1 2 3 n=4 +-------+-------+-------+-------+ +-------+-------+-------+-------+ 1 | (1,1) | (1,2) | (1,3) | (1,4) | 1 | (1,_) | (1,_) | (1,_) | (1,_) | +-------+-------#########-------+ +-------+-------#########-------+ 2 | (2,1) | (2,2) # (2,3) # (2,4) | 2 | (2,_) | (2,_) # (2,_) # (2,_) | +-------+-------#########-------+ +-------+-------#########-------+ m=3 | (3,1) | (3,2) | (3,3) | (3,4) | 3 | (3,_) | (3,_) | (3,_) | (3,_) | +-------+-------+-------+-------+ __ +-------+-------+-------+-------+ |\ /|\ x coordinates of each position: \ | \ | x 1 2 3 n=4 \ | +-------+-------+-------+-------+ the elements highlighted by #s 1 | (_,1) | (_,2) | (_,3) | (_,4) | show that if we pair up +-------+-------#########-------+ corresponding elements in 2 | (_,1) | (_,2) # (_,3) # (_,4) | <-- $x$ and $y$, we get back the +-------+-------#########-------+ positions of those elements m=3 | (_,1) | (_,2) | (_,3) | (_,4) | +-------+-------+-------+-------+ (we show how to compute $x$ and $y$ later) what is $x == y$? it is the following logical array: +-------+-------+-------+-------+ | 1 | 0 | 0 | 0 | +-------+-------#########-------+ | 0 | 1 # 0 # 0 | +-------+-------#########-------+ | 0 | 0 | 1 | 0 | +-------+-------+-------+-------+ observe that positions where the logical array is true are *exactly* on the diagonal and that $x == y$ is the equation/condition we saw above for the diagonal! in general, we can use a logical array to index into a matrix $s$. the positions selected in $s$ are exactly the positions where the logical array is true. thus, $s(x == y) = '\';$ is exactly what we need/want to assign $\$s to the diagonal. _______________________________________________________________________________ generating "coordinate matrices" $x$ and $y$ in general, how do we generate $x$ and $y$ for an m-by-n matrix? here is one way to get $x$: % +---+---+---+---+---+---+---+ x = 1:m; % x | 1 | 2 | 3 | ... | m-1 | m | % +---+---+---+---+---+---+---+ % we want: % + all columns in $x$, which we get using $:$, i.e. $x(?, :)$ % + row 1 of $x$, row 1 of $x$, row 1 of $x$, ..., row 1 of $x$ % \____________________________ ____________________________/ % V % $n$ copies of row 1 of x % which we get using $[1 1 1 .... 1] = ones(1,n)$ % x = x(ones(1,n),:); here is one way to get $y$, using the same idea: y = (1:n)'; y = y(:, ones(1,m)); note the use of the transposition operator $'$ to flip the row vector along the diagonal to make it into a column vector. however, it is simpler and easier to use the $repmat(A,M,N)$ function, which replicates $A$ many times -- $M$ times vertically, $N$ times horizontally: A repmat(A,4,5) --1-- --2-- --3-- --4-- --5-- +-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1|2|3| | |1|2|3|1|2|3|1|2|3|1|2|3|1|2|3| +-+-+-+ 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |4|5|6| | |4|5|6|4|5|6|4|5|6|4|5|6|4|5|6| +-+-+-+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |1|2|3|1|2|3|1|2|3|1|2|3|1|2|3| 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |4|5|6|4|5|6|4|5|6|4|5|6|4|5|6| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |1|2|3|1|2|3|1|2|3|1|2|3|1|2|3| 3 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |4|5|6|4|5|6|4|5|6|4|5|6|4|5|6| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |1|2|3|1|2|3|1|2|3|1|2|3|1|2|3| 4 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |4|5|6|4|5|6|4|5|6|4|5|6|4|5|6| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ so now we can get $x$ and $y$ as follows: x = repmat(1:n, m, 1); y = repmat((1:m)', 1, n); _______________________________________________________________________________ ! pros and cons of "useful trick" ! ! pro: shorter code, which can can be easier to understand ! (e.g. see solutions to E11) ! ! con: code can run slower, e.g. $\$s on diagonal in E11: ! ! + verbose loop code: ! ! for i = 1:m ! s(i,i) = '\'; ! end ! ! running time: $O(m) = O(n)$ ! ! ($n=2m$: throw away constant factor, $O(n)=O(m)$) ! ! ! + concise code pre-processing: ! ! x = repmat(1:n, m, 1); ! y = repmat((1:m)', 1, n); ! ! running time: $O(m*n) = O(n^2) = O(m^2)$ ! ! since must create each element of both m-by-n matrices $x$, $y$ ! ! ($n=2m$, so $n^2=4m^2$ -- throw away constant factor, $O(n^2)=O(m^2)$) ! ! + concise code after we have $x$, $y$: ! ! s(x == y) = '\'; ! ! running time: $O(m*n) = O(n^2) = O(m^2)$ ! ! + to compute $x == y$, matlab must look at all $m*n$ pairs of elements ! + to use a logical array as an index, ! matlab must scan all $m*n$ elements of logical array to ! find the true values ! ! + concise code: total running time is $O(n^2) = O(m^2)$ ! ! so, concise code is much slower than verbose code. ! ! ! is this a problem? ! ! + no, because in E11 $m$ is "small", so even "slow code" is pretty fast. ! ! + not in the case of E11, even if $m$ is "large": ! ! filling in the disk and filling in under the diagonal each take ! $O(m^2)$ time -- slow diagonal code doesn't make things any worse ! ! + maybe not in other cases, ! *if* $m$ is medium sized ! *and* it is easier/faster to write the concise code: ! ! *total* time spent on a program ! = running time ! + time it takes us to get (write or buy or whatever) the program ! ! so if we can write the program much faster, that might offset the ! extra time it takes to run. !