The aim of the supervision exercises is to get some practice approaching the types of questions you'll get in the exam.
Take a look at the function below:
function [output1] = Function1(input)
if input == 1
outpu1 = 'small';
else
output1 = 'big';
end
What outputs would be produced if we run the following commands
>> A = Function1(1);
>> B = Function1(-10)
>> C = Function1('one')
Would the code run correctly with these inputs, and if no errors are produced what would be the values of A
, B
, C
. Provide an explanation for why the function produced the given value.
Take a look at the function below. It is a slight variation on Function1.m
.
function [output1] = Function1(input)
if input == 1
output1 = 'small';
elseif input == 'one'
output1 = 'big';
end
What outputs would be produced if we run the following commands
>> A = Function1(1);
>> B = Function1(-10)
>> C = Function1('one')
Would the code run correctly with these inputs, and if no errors are produced what would be the values of A
, B
, C
. Provide an explanation for why the function produced the given value.
Write a simple function that produces the output shown given the input x
with the value shown. You might find it helpful to add comments to your code. e
>> x = [1:3];
>> CountFun(x)
one
two
three
>> x = [3:-1:1];
>> CountFun(x)
three
two
one
Write it such a way so that it will produce a sensible output for any input vector that contains the number 1
to 10
. You might find it helpful to add comments to your code.
Hints
for
loop, or use something like arrayfun()
disp()
).
'one'
) that corresponds to the number (e.g., 1
).Write a function that does the opposite to the function you wrote for Problem 1a. It must take cell
array of number words (see a below) and print out numbers
>> x = {'one','two','three'}
Write out a flow diagram for a basic computerised experiment measuring reaction times to a stimulus. You'll need to generate stimuli, present instructions, randomise the trials, collect responses, and save the results.
As a take home problem try to write a function that does the following.
vector
s of the same length numbers as inputvector
s'There is a significant difference between x and y'
or 'There is NOT a significant difference between x and y'
1
it is significant and 0
if it is notTo do this, you'll need to use the following functions:
mean()
: calculate a meansqrt()
: calculate a square rootstd()
: calculate a standard deviation (σ)scatter()
: a scatter plot/dot ploterrorbar()
: draw an error bar plot[h, p] = ttest()
: a one-sample t-test on a vector input (i.e., the difference scores).
h
is 1
if the difference is significant and 0
if it is not. The output h
is 0
if the difference is not significant.p
is the p-value.The formula for the 95% confidence interval is:
$${CI} = 1.96 \times \frac{\sigma}{\sqrt{n}}$$
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Hint: If you construct two vector
s with one containing the the multiple of 3
and one containing the multiples of 5
, then you can concatenate the two vector
s together and use the unique()
function to get ride of the duplicate values (e.g., 15
would be in both vector
s. If the input to unique()
contains two (or more) values of 15
the output will contain only 1 value of 15
.
x = 3 % define variable x to be 3
x = [1 2 3] % set x to the 1 x 3 row-vector (1,2,3)
x = [1; 2; 3] % set x to be the 3 x 1 column vector (1,2,3)
x(2) = 7 % change x from (1,2,3) to (1,7,3)
s = 'a string' % make the varibale s equal the string 'a string'
c = {'one','two','three'} % define a cell aray (c) containing three strings
rand(12,1) % a 12 x 1 matrix of numbers in uniform distribution of [0, 1)
randn(12,1) % a 12 x 1 matrix of numbers from a normal distribution with mean 0 and variance 1
randi(10,12,1) % a 12 x 1 matrix of random integers between 1 and 10
randperm(12,1) % 10 unique random numbers betweeen 1 and 12
zeros(12,1) % a 12 x 1 matrix filled with zeros
x(2:12) % the 2nd to the 12 elements of x
x(2:end) % the 2nd to the last element of x
x(1:2:end) % every 2nd element of x from the first to the last
x(:) % all the elements of x
x(5,:) % the fifth row of x
x(:,2) % the 2nd column of x
sum(x) % calculate the sum of the elements in x
mean(x) % calculate the mean of x
x .^2 % calculate the square of each element in x if x is a vector
x ^2 % calculate the square of x if x is a single number
sqrt(x) % calculate the square root of the number x
if x == 1 % if variable x is equal to 1
FunctionA % then run Function A
elseif x == 2 % or if x is equal to 2
FunctionB % then run Function B
else % otherwise
FunctionC % run Function C
end
for i = 1 : 4 % loop 4 times
FunctionA(i) % over Function A using i as the input
end
k = 0; % set the variable k to zero
while (k < 5) % loop until k is equal to 5
FunctionA % run Function A
k = k + 1; % increase the value of k by 1
end
if strcmp(s,'a string') == 1 % is s contains the string 'a string'
FunctionA % run Function A
end
arrayfun(@(x) FunctionA(x),TheArray) % run the function FunctionA using each element of the array named TheArray as an input
cellfun(@(x) FunctionA(x),TheCells) % run the function FunctionA using each element of the array named TheArray as an input
plot(x) % plot y on the y axis with 1, 2, 3,... as the x axis
plot(x,y) % plot y vs x (x and y must be the same length)
axis equal % force x and y axes to be scaled equally
title('A title') % change the plot title to A title
xlabel('x units') % change the x axis label to x units
ylabel('y units') % change the y axis label to y units