IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

19
IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiaris m

Transcript of IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Page 1: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

IT1005

Lab session on week 8 (4th meeting)

Say No to Plagiarism

Page 2: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Email Submission System (Review)

• Good, just one-two “email subject” violations for lab 4 . – For those who are still blur, write “IT1005-2B YourName” in your

email subject! This helps my Gmail to auto sort your submissions.• Now, I have one more request.

– For lab 5, instead of naming “u0xxxxxx-lab5.zip”,write in your name!• “StevenHalim.zip”, “StevenHalim.doc”, “StevenHalim_q51.m”, “StevenHalim_q52.m”,

“my_mean.m”, “my_var.m”, “my_sort.m”, “my_sort2.m”.• Ignore Colin’s instruction, I am using my own standard here.• DO NOT COPY THE INDIVIDUAL *.m FILES INTO *.doc!

– No need to send me duplicate things >.<– Your MS-Word file will just contain ‘explanation’ of your code.– Or you can just embed that explanation as comments inside your m files,

no need to send me an MS-Word file for this lab…

– Reason:• I cannot grade one student per student, too slow…• It is better/faster to grade one question per question…• So I need to download your files individually anyway.• With so many files for lab 5, it will be a chaos if all of them are ‘u07xxxxx’! ><

Page 3: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Plagiarism• What are NOT ok?

– Blindly/partially copy your friend’s M-file/MS-Word file with/without his/her consent!– ‘Just’ looking at other student’s MS-Word file, your answer will then tend to follow his/hers,

especially if you are not sure with your own answer…– Working together to create the M-file, then copy-paste the SAME M-file to each of

your MS-Word files.– Blind/partial copy from past year solution... It happens for lab 4 q2, q3, and q4.

• Anyway, past year solution is not always 100% correct according to current grader (me)…

• What are ok?– Gentle discussion: there is only thin line between plagiarism and proper discussion.– Positive discussion is ok, and it helps learning.– After ‘gentle’ discussion, you must type in Matlab code from scratch by yourself!– From your discussion, create two different Matlab programs that do the same thing!

• That shows me that you really understand what you are doing.

• This is a strong warning for Lab 4!– No more mercy for lab 5 onwards!– Plagiarism will just make your life more difficult for upcoming labs/assignment/final exam!– Note that labs is just 10%, midterm test is 20%, assignment is 30%, final is 40%!

Say No to Plagiarism

Page 4: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Quick Check• Have you all received my reply for lab 4?

– My reply contains:• Your Microsoft Word file• With my comments, and• Your marks.

– If there is something wrong with the marks, clarify it with me.

– Note that your file is renamed to YOUR matriculation card name.– Help me by naming your files for lab 5

(the zip file, the Microsoft Word file) with your name!• See the previous two slides for details.

Page 5: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q1)• Q1 – Mastering the command ‘find’

A. [r c] = find(F < -0.8)B. Either you write 4 coordinates: (2,1) (2,2) (1,3) (2,3),

or you create program to enumerate these.for index=1:numel(r) % if r has 4 elements, it will display 4 lines disp(sprintf('(%d, %d)', r(index), c(index)));end

C. [r c] = find(-1 < F & F < -0.8) % computer can only do BINARY comparison!% Note: -1 < F & F < -0.8 is a better style compared with F < -0.8 & F > -1% as it clearly shows the range that F is in.% The resulting coordinates (not asked): (2,2) (1,3) (2,3).

Page 6: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q2)• Q2 – for loop versus while loop

A. Observations:• Loops ‘max’ number of times, each time prints the value of i and cnt, then finally prints the value of cnt / i.• When swapped, ‘i' tells the value of ‘cnt’ in scientific notation (cannot print floating point using %d),

‘cnt’ tells the value of ‘i‘, which is not really correct if you just look at the output.The final value of ‘cnt’/i does not change since the physical ‘cnt’ actually never altered!

• ‘\n’ is newline character.

B. While loop version of the same program:1. max = input('please enter maximum count: ');2. cnt = 0; i = 1; % must set the initial value3. while i <= max % keep looping while this statement is still true4. cnt = cnt + 0.125 * i; % if you start from i = 0; do i = i + 1; before executing this line!5. sprintf('i is %d and cnt is %g\n', i, cnt)6. i = i + 1; % must do this so that after looping ‘max’ number of times, condition in line 3 is no longer true.7. end8. cnt = cnt / (i - 1); % we will over count by one after exiting the while loop! Alternative: loop from 0 to < max!9. sprintf('cnt divided by i gives %g\n', cnt)

C. Differences are the one highlighted in part b above. See the comments there.• Some of you are not really answering the question: for loop is “bla bla”, while loop is “bli bli”.• The context is q2b! Say that the while loop version needs to have i initialized to 1 outside the loop, incremented by one inside the

loop, and stopped when i <= max, it will be over count by 1 at the end, etc…

Page 7: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q3-standard)% This may be your first ever Matlab program with repetition control flow.

num = 0; denom = 0;n = input('Please enter the number of modules: ');

% with just ONE loop, you can get input, and compute the results simultaneously. Assume VALID inputs!for moduleID = 1:n % it is better to use ‘for loop’ as we know how much to count here (1 to n).

% use meaningful variable name, e.g. ‘MC’, ‘grade’, rather than ‘f’, ‘s’ (do not follow the lab hints too much >.<)?MC = input(sprintf('Please enter the modular credit for module %d: ', moduleID));% denom can already be updated now.% num must be latter, since it requires both ‘MC’ and ‘grade’(sequence issue!)denom = denom + MC;grade = input(sprintf('Please enter the grade point for module %d: ', moduleID));num = num + MC * grade;

end

cap = num / denom;disp(sprintf('CAP = %g\n', cap)); % better use ‘disp’ so that the ‘ans’ is not printed!

Page 8: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q3+protection)num = 0; denom = 0; n = 0; % initialize n properly!

while n < 1n = input('Please enter the number of modules (integer >= 1): ');

end

for moduleID = 1:nMC = -1; grade = -1; % initialize MC and grade properly!

while MC < 1MC = input(sprintf('Please enter the modular credit (integer >=1) for module %d: ', moduleID));

enddenom = denom + MC;

while grade ~= (0:0.5:5) % complex statement, this is to test whether grade is one of the valid option?grade = input(sprintf('Please enter the grade point (floating point within [0.0 .. 5.0]) for module %d: ', moduleID));

endnum = num + MC * grade;

end

cap = num / denom;disp(sprintf('CAP = %g\n', cap));

Good that some of you remember that I have

mentioned extreme cases during lab 0/1 (the division

by zero stuff).

Page 9: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q4-standard)% This may be your first ever Matlab program with decision control flow.

… continued from q3 % Please… DO NOT give me the answer for q3 again! Redundant! % Either write this line “continued from q3” or just combine your q3 and q4 answers!

% use if-elseif-elseif-elseif-end rather than using nested if-else-end (too skewed to the right and you may confused with ‘end’s!)if cap > 4.5 % what if cap == 4.5?

disp('Genius!!');elseif cap >= 4.0 & cap < 4.5 % is this cap < 4.5 needed?

disp('Pretty darn smart!!')elseif cap >= 3.5 & cap < 4.0

disp('Smart!');elseif cap >= 3.0 & cap < 3.5 % how to explain the overlapping ranges?

disp('Quite Clever!');elseif cap >= 3.2 & cap < 3.5 % this is subset of the above

disp('Clever!');elseif cap >= 2.5 & cap < 3.2 % 3.0 – 3.2 are already classified as ‘Quite Clever!’ above

disp('Hmmm…');elseif cap >= 2.0 & cap < 2.5

disp('Ohh need to work harder!');elseif cap < 2.0

disp('Oops.. ');end

Page 10: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 4 – Answers (Q4-better)… continued from q3

if cap >= 4.5 % what if cap == 4.5? Assume it is genius (first class), cap value cannot be > 5.0 anywaydisp('Genius!!'); % if our previous inputs are valid (see slide 7, q3+protection).

elseif cap >= 4.0 % cap < 4.5 is NOT needed as it is true if it does not fit into the first case above!disp('Pretty darn smart!!')

elseif cap >= 3.5disp('Smart!');

elseif cap >= 3.2 % reordered, ‘Clever!’ is cleverer than ‘Quite Clever!’disp('Clever!'); % new range = [3.2 .. 3.5)

elseif cap >= 3.0 disp('Quite Clever!'); % new range = [3.0 .. 3.2)

elseif cap >= 2.5 disp('Hmmm…'); % new range = [2.5 .. 3.0)

elseif cap >= 2.0disp('Ohh need to work harder!');

else % obviously this case is cap < 2.0disp('Oops..');

end

% Note: there are other ways to answer this question, all are ok as long as proper assumption is given!

Page 11: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

The Art of Indentation (1)

• Indentation depth = 1 tab, 2/3/4 spaces– It is a matter of style, stick with one and be consistent!

• Increase the indentation level after – Decision statements (‘if’, ‘switch’), or– Repetition statements (‘for’, ‘while’)!

• Decrease the indentation level– As soon as you hit an ‘end’ statement.

• If you still unsure, study the next few slides!

Page 12: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

The Art of Indentation (2)

• Wrong Example• Too flat:

a = 2;if a >= 1b = 0;while b < 7disp(‘hey’);b = b + 1;endenddisp(‘last line’);

• Wrong Example• Zig Zag:

a = 2;if a >= 1

b = 0;while b < 7disp(‘hey’);

b = b + 1;end

enddisp(‘last line’);

Page 13: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

The Art of Indentation (3)

• Wrong Example• Indenting if/while:

a = 2;if a >= 1

b = 0;while b < 7

disp(‘hey’);b = b + 1;

endenddisp(‘last line’);

• Wrong Example• Wrong match:

a = 2;if a >= 1

b = 0;while b < 7

disp(‘hey’);b = b + 1;end

enddisp(‘last line’);

Page 14: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

The Art of Indentation (4)

• Wrong Example• Too many white spaces:

a = 2;if a >= 1

b = 0;while b < 7

disp(‘hey’);b = b +

1;end

enddisp(‘last line’);

• Good Example• Balanced, neat:

a = 2;if a >= 1

b = 0;while b < 7

disp(‘hey’);b = b + 1;

endenddisp(‘last line’);

Indentation level 0

Indentation level 1

Indentation level 2

Page 15: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Review on Functions• So far, codes that we have written reside in ONE M-file only.

– As the code getting more complex, it can be very messy.

• We need to ‘modularize’ them into logical functions.– Group of codes that can be reused again in the future.

• Example of functions that we have used so far:– sin, cos, abs, input, disp, sort, max, min, etc.

• The syntax is easy, just remember this:function returnValue = functionName(argument1, argument2,…,argumentN)% If you want to add help messages, write it here. Separate it with blank line!

% Write your Matlab codes here, get the arguments, process them, return the result! % Usually, functions do NOT ask for input/print output! (exception: input, sprintf, etc)% Usually, we suppress all the ‘intermediate’ output by giving ‘;’ at every linereturnValue = finalValue; % assign the result to this variable (returned to caller)!

• Save the function into a new M-file– With exactly the same file name as the function name!

Input(s) to your function

Output(s) of your function

Page 16: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Example (Related to Lab 5)

• Create a function “my_max.m” that given an array of numbers,return the maximum number inside the array.

• Example:– A = [ 1 3 ; 4 2 ]; % This is an example with 2-D array– % Not all Matlab commands behave similarly for 1-D (row array) versus 2-D!– my_max(A)– ans =– 4

• Testing:– Compare our own implementation with Matlab’s default ‘max’!– my_max(A) == max(max(A)) % must do max(max(A)) for 2-D array!– my_max(A) == max(A(:)') % or do this , clever trick for any dimension!

• If ans = 1 for various types of A, likely that you have done it correctly =).• If ans = 0 for an instance of A, then you are still wrong.• This is ok since my input are all Integers

– The test is different if the input is not integers! % test if absolute difference < eps

Page 17: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

my_max.mfunction maxValue = my_max(inputArray)% my_max(inputArray)% This function will return the max element of inputArray% Only one argument will be accepted...% This is the last line of the help message... % Find the max element in inputArraymaxValue = inputArray(1); % Assume that the first element is the maxfor index = 2:numel(inputArray) % Iterate through the rest to find the true max if inputArray(index) > maxValue maxValue = inputArray(index); endend

% Look, I do NOT assign returnValue = maxValue as the last line of my function% I can use maxValue (return value) like normal variable, highlighted in red :)

Page 18: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 5 - Overview• Sample Mean = sum all n items divided by n• Sample Variance = (standard deviation)2 =

sum of squared deviation divided by (n – 1)– Not by ‘n’.

• There is a reason for this. *See Statistics textbook*

• Sorting– Given unsorted input, e.g. [5 2 3 1];– Do something with these numbers (sort it).– Return the sorted output of the input, e.g. [1 2 3 5];

• Bubble sort = one of the ‘easiest’ (but slow) sorting algorithm.– Google this term to study more!

• rand [0.0 .. 1.0] versus randn (normalized with mean 0, var 1).– randn(x) will give you n*n matrix of random numbers

• You need to do something else to create row array! % randn(1,number_of_columns)

Page 19: IT1005 Lab session on week 8 (4 th meeting) Say No to Plagiarism.

Lab 5 – Free and Easy Time• Now, you are free to explore Matlab,

especially to find answers for Lab 5.• No extra challenge this time >.<• Let’s just focus on finishing lab 5 on your own,

catch up if you are still behind,or to do your 30% term assignment .

• PS: Now we are entering the second half of the semester.Your lecturer have changed to Dr Saif Khan.And the topics shifted to ‘applications’ of Matlab .