% Assignment 2 - functions and flow control % % Nigel Stepp % % It might help to paste some of these things into their own files. % If you have questions, or want comments, feel free to email me % your answers. % % *** Note: if Question 1 is seeming too hard, just skip it and we can % go over it in class. *** % % Topics covered: functions, input and output parameters, flow control % Question 1: % % An undergrad messed up your data files, and instead of having % a column for each variable that you are measuring (3 of them), there % is just one long column. % % But, you know you took 5 observations of each variable! (we'll assume % that 5 observations is enough :) ) % % Since you know the number of observations, and the undergrad fortunately % kept the data in order, you should be able to reconstruct your data matrix. % % Take the data in 'bad_data' and create a 5x3 matrix of your % corrected data called 'data'. % % hint: you may need to *iterate*. % other hint: if you want to be extra elegant, consider using % the 'mod' builtin function which computes the modulus. % If you use mod, you will want to look up 'ceil' as well. bad_data = [5,8,3,4,5,10,14,27,19,2,18.2,72,89,105,37]; % 'data' should end up like this: % 5 10 18.2 % 8 14 72 % 3 27 89 % 4 19 105 % 5 2 37 % Question 2: % % Great, looks like this poor undergrad did this with *all* of % your data files. So you have to run this on all of them. % % Write a function which takes a bad_data vector as an input parameter % and returns a corrected matrix. % % hint: This is just an extension of Question 1. % % If you want to be really general, also make the function take % the number of variables and the number of observations as % additional input parameters. In Question 1, this would be % 3 and 5, respectively. % Question 3: % % Here is a while loop which asks the user for input. Notice that % it loops while true is true (forever!). This is called an infinite % loop. % % Add code where requested. % % This uses the builtin functions 'input', 'disp', and 'strcmp'. See MATLAB help % for their uses (it's good to get in the habit of using it as a reference). while true entry = input('Please type something and hit enter: ', 's'); if strcmp(entry, 'Knock knock') disp('Who is there?'); end; if strcmp(entry, 'What is your name?') disp('Hi, my name is MATLAB, thank you for programming me.'); end; % Here, place some code that will stop the loop if the user % enters 'quit'. You might want to say goodbye, also. disp( ['(By the way, you entered "', entry, '")'] ); end;