MATLAB Data Containers and Function Handles: Practice Questions

Describe the variable type and value of the variable A after executing each of the following statements

1)  A = [2:-2:-2];

2)  A = logical([2:-2:-2]);

3)  A = exp([0, 1, -1]);

4)  A = {2, 'hello world'};

5)  A = struct('field1', 16, 'field2', 17);
    

6) What are the values of the variables x, y, z and w after executing the following Matlab script?

x = 1; y = 2; z = 3;
w = myfunc(x,y,z,3,4);

function z = myfunc(x,y,s,v,w)
x = s + v;
y = s - v*w;
z = x + y;
end
    

7) What are the values of the variables x and a after executing the following Matlab script?

x = 1;
[x, a] = foo(x,7,-1);

function [x, z] = foo(s,x,w)
z = s + w;
x = x - s;
end
    

8) What is the value of the variable x after executing the following Matlab script?

myfh = @(x,y)(y*exp(x));
x = myfh(0,7);
    

9) What numbers will be displayed when the following Matlab script is run?

fh = @(x,y,z) afunction(x,y,z);

for k = 1:2:3
    [p, q] = fh(k,-k,3);
    display(q)
end

function [x, y] = afunction(s,t,u)
x = s + t;
y = s + u;
end
    

10) A Matlab function can be used recursively i.e., call itself during its execution. Which mathematical function is implemented by the Matlab function below?

function result = my_recursive_function(n)
if n == 0 || n == 1
    result = 1;
else
    result = n * my_recursive_function(n - 1);
end
    


For more details, please contact me here.