%% LESSON 9: Program control % % *FOCUS QUESTION: How can I execute different code depending on the data?* % %% EXAMPLE 1: Simulate tossing a coin (selection using |if-else|) toss = rand(1, 1); % Pick a value at random between 0 and 1 if toss <= 0.5 % Test against the value 0.5 fprintf('Tossed heads\n'); % Say its heads if toss is less 0.5 else fprintf('Tossed tails\n'); % Say its tails if toss is less 0.5 end; %% EXAMPLE 2: Output the square roots of first 3 integers (simple |for| loop) for k = 1:3 % The loop index k takes values 1, 2, 3 fprintf('sqrt(%g) = %g\n', k, sqrt(k)); end; %% EXAMPLE 3: Sum the square roots of the first 10 integers (accumulation using a |for| loop ) sumSqrts = 0; % Need a variable to accumulate sum for k = 1:10 % Loop over the values k = 1, 2, ... 10 sumSqrts = sumSqrts + sqrt(k); % Add the next sqrt root to total end; fprintf('Sum of square roots from 1 to %g is %g\n', k, sumSqrts); %% EXAMPLE 4: Simulate tossing coin 50 times (|for| loop with selection and accumulation) numTosses = 50; % Number of times to toss the coin numHeads = 0; % Need a variable to accumulate total heads for k = 1:numTosses % Loop over the values k = 1, 2, ... numTosses if rand(1, 1) <= 0.5 % Add to head count if 'tossed a head' numHeads = numHeads + 1; end; end; fprintf('%g heads in %g tosses\n', numHeads, numTosses); %% EXAMPLE 5: Alternative implementation of coin toss simulation (vector indexing) timesToTosses = 50; % Number of times to toss the coin randTosses = rand(timesToTosses,1); % Create vector of "tosses" numHeads = sum(randTosses <= 0.5); % How many were heads? fprintf('%g heads in %g tosses\n', numHeads, numTosses); %% EXAMPLE 6: Load the sleep diary data load diaries.mat; % Load the sleep diaries %% EXAMPLE 7: Output a message if any subjects awoke after 3:30 pm wakeHours = (wakeTimes - floor(wakeTimes))*24; % Calculate the wake-up hours lateWakeup = sum(sum(wakeHours > 15.5)); % How many late wake-ups? if lateWakeup > 0 % See if any late wake-ups fprintf('%g wake-ups after 3:30 pm\n', lateWakeup); end; %% EXAMPLE 8: Output subject number and gender for subjects with at least 1 wake-up after 3:30 pm timesLate = sum(wakeHours > 15.5); % Times each subject woke up late fprintf('Subjects who had a least one wake-up after 3:30 pm:\n'); for k = 1:length(timesLate) if timesLate(k) > 0 fprintf('Subject %g: a %s with %g late wake-ups\n', ... k, gender{k}, timesLate(k)); end; end; %% EXAMPLE 9: Output the subject number and gender of the first student in section 3 (break) sect3 = (section == 3); % True (1) for subjects in section 3 for k = 1:length(sect3) % Here k = 1, 2, ... subject number if sect3(k) % If subject is in section 3 fprintf('First subject in section 3 is a %s with subject number %g\n', ... gender{k}, k); break; % Get out of the loop, we done end; end; %% EXAMPLE 10: Using |find| to perform same task as EXAMPLE 9 pFirst = find(section == 3, 1, 'first'); % Find position of first subject in section 3 fprintf('First subject in section 3 is a %s with subject number %g\n', ... gender{pFirst}, pFirst); %% EXAMPLE 11: Output sections that have subjects with late wake-ups (continue) wakeHour = 12; % Wake hour to check averWake = mean(wakeHours); % Compute the average wake up time for all subjects for s = 0:3 % Check each section (0, 1, 2, 3) sectWake = averWake(section==s); % Pick out wakeups for section s numLate = sum(sectWake >= wakeHour); % How many are after wakeHour? if numLate == 0 % Skip this section if no late ones continue; end; fprintf('Section %g has %g subjects with mean wake-up after %g\n', ... s, numLate, wakeHour); end; %% EXAMPLE 12: Output a table of early wake-ups using a loop earlyWake = 6; fprintf('\n\n\tEarly wake-ups\n'); fprintf('Subj\tSect\tGender\tAver Wakeup\n'); % Print out a title for k = 1:length(averWake) % Here k = 1, 2, ... subject number if averWake(k) >= earlyWake % Skip subjects who awoke later continue; end; fprintf(' %g\t %g\t%s\t %g\n', k, section(k), gender{k}, averWake(k)); end;