Lab session on week 7 (3rd meeting) Welcome back from Mid semester break =) (a. k. a Mid test period) IT1005 Remember This (Again)! • E-mail your answers (in Microsoft Word format) to: stevenhalim at gmail.com • With EMAIL subject: – “IT1005-LabGroupID” “FullName”, • e.g. IT1005-7A StevenHalim – Mr Gmail will sort your emails for me =) – Wrong subject header will confuse Mr Gmail • Try to submit your answers ONCE before deadline! – Do not submit again to fix some minor mistakes or to ‘reconfirm’! • Shorter (but correct) answers ~> better marks =) • Remember, I have ~90 emails/week ! Violations • There are several violations for lab 3… – Mostly by those who do not actually attend my classes… • I have decided that there is no penalty for lab 3… – Only late submission get -1 mark (per day) penalty. • For lab 4 onwards, no more mercy… – Every violation for this e-mail submission mechanism will result in deduction of 1 mark from the offender’s marks for that particular lab. Lab 3 – Answers (1) • Q1 – Operations on Matrix A. Results: • • 3*3 matrix; [ ] create array; `;’ change row 0:2:4 start from 0, increment by 2, stop until 4, etc. B. Several options: • • • Manual, not advisable, B = [0 2*pi 4*pi; … ]; or B = [0 2 4; …] * pi; Semi manual, B = [0:2*pi;4*pi; …]; or B = [0:2:4; …] * pi; Assuming A exist: B = A*pi; or B = A.*pi; (‘.*’ is actually not needed as pi is scalar). C. Several options, if I am the lecturer, I will ask you to list at least 4 ways! • • • • • • • Wrong, C = [16*pi 14*pi 12*pi; …];, not using matrix B as reference! Manual, not advisable, C = [B(3,3) B(3,2) B(3,1); …]. Use loop/index, C = B(3:-1:1,3:-1:1); C = B(end:-1:1, end:-1:1); or C = B([3 2 1],[3 2 1]); Use Math, C = 16*pi - B; or C = repmat(16*pi, 3, 3) - B; Use sort, C = sort(sort(B,1,'descend'),2,'descend'); Use rotate, C = rot90(B,2); or C = rot90(rot90(B)); Use flipup and fliplr, C = fliplr(flipup(B)); Lab 3 – Answers (2) • Q2 – Operations on Matrix A. D = A(:) • • • D is a 9*1 column vector. Listed column by column or according to the indexes of each element. Please specify the order! Otherwise I can say zig-zag/random/row by row patterns! B. A(1,:) = repmat(-2,1,3) • • • There are several ways to execute this repmat function! A(1,:) = repmat(-2,1,1) also works… Why? A(1,:) = -2 also works… Why? (but this one is wrong since it does not use ‘repmat’). C. A(:,2) = zeros(3,1) + pi • • • • – There are several ways to write the zeros function. Actually ‘zeros’ is redundant here! A(:,2) = zeros(1,3) + pi also works… Why? A(:,2) = zeros + pi also works… Why? A(:,2) = pi also works… Why? (but this one is wrong since it does not use ‘zeros’). 2B & 2C: scalar expansion is ok, but to avoid confusion, let’s try to be precise! Lab 3 – Answers (3) • Q3 – Plotting multiple graphs on the same figure A. Observed: • Two graphs on one figure. – • • We can do the same thing with ‘hold on’! I have shown it 2 weeks ago (‘creating flatland’). Say that y is sine graph, z is cosine graph within domain: -2pi to 2pi, range: -1 to 1. The color of the graphs are blue and green because of Matlab settings. – You can actually specify plot colors! plot(x,y,’r’,x,z,’m’); % r -> red, m -> magenta. Try! B. Several options: • • • Bad, create E from scratch. Assume B exists, then create E from B by dividing B with ./ [1 2 3; 4 5 6; 7 8 9]. Apply F = sin(E) element by element. C. Several options: • • • plot(E(:,1),F(:,1), E(:,2),F(:,2), E(:,3),F(:,3)); grid; plot(E,F); grid; This also works because Matlab access matrix column by column. We observe three not so precise sine graphs on one figure. • No relational or logical questions actually… These are for lab 4… Midterm test answers • Only if at least 5 students want to know the ‘truth’ 1. 2. 3. 4. 5. 6. 7. 8. B. 3.0 GHZ, 1 GB DDR-RAM is still faster than 3.2 GHZ, 1 GB DRAM. D. ‘0’/‘1’ only. B. x=1, y=0, z=1. E. None of the above. B, C, D are definitely wrong (not binary), A is also wrong because ‘63’ is one less from ‘64’ (exact multiple of ‘2’), thus it will not have ‘0’ just before the fraction point. B. Step refinement. Coarse, fine grained, etc. D. Little_three_cats are the only valid variable, try ‘isvarname(Little_three_cats)’. B. x is 20. both ‘long variable name’ > 63 chars will be truncated into max 63 chars, thus both looks the same for Matlab. Thus x = 10 + 10. D. SimpleSimon%=3; % is comment, so this is basically print out the current value of SimpleSimon (assumed to be a valid variable, as stated in the problem description). Midterm test answers • Only if at least 5 students want to know the ‘truth’ 9. A. x is 3.8278, apply the BODMAS! Just be careful with \ (reverse operand) 10. B. A will contain values between 31 and 61 inclusive, in increments of 2. This is due to scalar expansion. 11. E. None of the above. A and B are not scalar, thus no scalar expansion. The ‘.*’ is element by element multiplication matrix dimension must agree. 12. D. C is a 3x3 matrix containing pi in all elements except the diagonal (2+pi). eye identity matrix [1 0 0; 0 1 0; 0 0 1]! 13. A. C contains the value 1. A(2:-1:1, 2:3) will produce [5 6; 3 4] Searching for 5 in the linear version of A {5 3 6 4} returns index 1. 14. D. A = [1 3 5; 2 4 6; 1 2 2]; Similar as our lab 3. 15. D. X = 2. Just simulate the process. 16. E. Colin Tan and Saif Khan. Too bad if you get this wrong :p. What we have learned so far • Top Down Design to create programs – Big steps, small steps, comments, Matlab codes. • Matlab program itself – Command/figure window, workspace, debugger, etc. • Things about Matlab array/matrix – Creation, Deletion, Access, Update, Operations, etc. • Math and logic operations – BODMAS, Relational (>, ==, <, etc), Logical (And, Or, Not). • Is it enough to create more complex programs? • So far our programs are straight from start to end. – Single sequence… Review on Control Structures • Sequence – Superman example (wrong sequence of something important)… – How to exit from this room? • Repetition – for-end if your repetition related to ‘counting’ – while-end if you need to repeat until some event occurs… – e.g. Count sum of square numbers from 1 up to n2: 1+4+9+…+n2 = ? • Selection – – – – if-end only test one condition if-else-end one condition versus ‘the rest’ if-elseif-else-end several conditions switch several equality conditions, not ok for INEQUALITIES! Quick Test on Control Structures • • Decide what to do if an earthquake happens HERE and NOW! Simulate an ATM machine that can do these simple operations: 1. Dispense money 2. Check PIN 3. Ask amount to be withdrawn 4. Deduct the user’s account 5. Ask for ATM card ??? err… something fishy… ??? Lab 4 – Free and Easy Time • Now, you are free to explore Matlab, especially to find answers for Lab 4. • For those who have done Lab 4, try this extra challenge! The solution is at the end of this session… – Let’s create our own ATM machine . – Operations as listed previously (now in logical order): 1. Ask for card 2. Check PIN 3. Ask amount to be withdrawn 4. Dispense that money 5. Deduct the user’s account