220 likes | 236 Views
IF. IF something is true do {list of commands} Else { list of commands2} end. IF syntax. if (conditional statement) … elseif (conditional statement) … else … end . Conditional statements have RELATIONAL Operators p. 8:
E N D
IF IF something is true do {list of commands} Else { list of commands2} end
IF syntax if (conditional statement) … elseif (conditional statement) … else … end Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…
Exercise 1 Write a program that simulates flipping a coin and then tells heads or tails. Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…
Exercise 1 (ex1.m) number = round(rand); if number == 1 text = 'heads'; else text = 'tails'; end text
Exercise 2 Write a program that creates a 10 by 4 matrix that contains 1 to 10 on the first column and zeros on the second column using a for loop. Then, assign the following values on the second column. 1-3: 1, 4-6: 2, and 7-10: 3.
Exercise 2 (data.m) A = zeros(10,4); for i = 1:10 A(i,1) = i; if i < 3.5 A(i,2) = 1; elseif 3.5 < i & i < 6.5 A(i,2) = 2; elseif 6.5 < i & i < 10.5 A(i,2) = 3; end end A
Exercise 2 Cont. Create another column filled with random numbers between 1 and 3.
Exercise 2 (dataV2.m) for i = 1:10 A(i,3) = round(rand*2)+1; end A
Exercise 2 Compare values of 2nd and 3rd columns for each row, assign 1 on the 4th column if they are the same and 2 if they are different.
Exercise 2 (dataV3.m) for i = 1:10 if A(i,2) == A(i,3) A(i,4) = 1; else A(i,4) = 2; end end A
WHILE A loop for an a priori unknown number of iterations. WHILE (condition is satisfied) keep doing this. End
WHILE: syntax while (condition) %if condition is false, loop is not executed, “jumps to end.” … if (condition) break %exits while end … end
Exercise 3 Count how many while loops the computer take to randomly find a number > .99 Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…
randcount.m counter = 1; a = rand; while a < .99 a = rand; counter = counter + 1; end a counter
AND/OR logic • used in if and while loops: if A is true & B is true … end
AND/OR logic • be careful about infinite loops with OR A = 1; count = 0; while A < 60 | count > 50 A = A+1; count = count+1; end Can use Ctrl C to end program if it gets stuck while running
Exercise 3b • Change the while loop in Exercise 3 to exit early if it’s looped more than 50 times. You can do this using either AND or break.
randcount.m counter = 1; a = rand; while a < .99 & counter < 51 a = rand; counter = counter + 1; if counter > 50 break; end end a counter
Exercise 4 • Create your own ‘randperm’ using a while loop.
own_randperm.m clear all input = 8; A = zeros(1,input); rand_number = round(rand*input); for i = 1:input; while ismember(rand_number, A) rand_number = round(rand*input); end A(1,i) = rand_number; end
Homework 1.Write a script encrypt.m that encrypts words. --> don’t forget to add headers and comments to your code. 2. Write MessWord.m This program takes a word as input and shuffles only the inner letters of the word, leaving the first and last letter undisturbed.Alejandro --> Arjelando House --> Husoe
MessWord What are the steps to undertake? 1. Figure out if the word is valid (no characters or numbers) 2. Figure out if the length is valid (if length < 3, what should happen?) 3. Use your old homework to do the rest.