How to use nested functions to build a code that compares inputs

I am somewhat of a newbie. I am writing a code that acts as an autograder with the following requirements. The function tests two functions (the student's solution and the instructor's reference solution) by calling them repeatedly with various input arguments and comparing the results. Both functions are assumed to take exactly one input argument. The inputs are two function handles followed by a variable number of additional input arguments. This grader function must call the two functions with each of the supplied input arguments one by one. If the results match all test cases, i.e. for each input argument, the grader function returns logical 'true'. Otherwise, it returns 'false'. Comparison must work for both arrays and scalars.
I have been working on the following code, which works for inputs like:
grader(@sin,@max,0)
grader(@sin,@max,0,1)
but not for inputs like:
grader(@cos,@cos,0,1,[-pi,0,pi])
Please help! Thank you.
function result = grader(a,b, varargin)
result = true;
in = varargin;
for ii = in{1}:in{end}
A = a(ii);
B = b(ii);
end
if isequal(A,B)
result = true;
else result = false;
end
end

3 Comments

Although the title is "How to use nested functions to build a code that compares inputs", nothing in your question seems to be related to nested functions: https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html
Note that the colon operation here:
in{1}:in{end}
is very unlikely to be useful for you: in some cases you will get an error, in other cases you will get a vector of values which may or may not be provided as input values. In MATLAB it is usually best to iterate over indices, not data values.
Wow, thank you for your help. Now that you mentioned it, would a plausible solution consist of a nested function? Say, the function that compares the results -- can it be nested within the main grader function?
Ah okay, nevermind. Thanks again!

Sign in to comment.

 Accepted Answer

The comparison needs to be inside the loop (not after the loop):
function result = grader(funA, funB, varargin)
result = true;
for k = 1:numel(varargin)
A = funA(varargin{k});
B = funB(varargin{k});
if ~isequal(A,B)
result = false;
return % no point in continuing, might as well exit now.
end
end
Giving:
>> grader(@sin,@max,0)
ans =
1
>> grader(@sin,@max,0,1)
ans =
0
>> grader(@cos,@cos,0,1,[-pi,0,pi])
ans =
1

6 Comments

Thankyou , so much stefen it helped me to submit my assignment.
function grader(a,b, c)
testcode(a, c);
testcode2(b, c);
if isequal ( testcode== testcode2)
true
else
false
end
function valOut = testcode(funcIn , numIn)
valOut = funcIn(numIn);
end
function output = testcode2( func, value)
output = func(value);
end
end
somehow this code will run? I am not able to compare the functions
Can someone tell me why this code is giving me an error
function result = grader(st_fun , ins_fun, varargin)
result = true;
for ii = 1:nargin
var_1 = st_fun(varargin{ii});
var_2 = ins_fun(varargin{ii});
if ~isequal(var_1,var_2)
result =false;
return
end
end
end
It showed index exceeds the number of array elements. Index must not exceed 1.
error in grader(line 4)
var_1 = st_fun(varargin{ii})
"Can someone tell me why this code is giving me an error"
for ii = 1:nargin
% ^^^^^^ because of this.
Count how many inputs your function has. Count how many elements VARARGIN has.
'nargin' not only counts the input values of 'varargin', but also the other inputs (st_fun, ins_fun)
You need to either substract the amount of other inputs:
for ii = 1:(nargin-2)
% In this case 2 because of the other 2 inputs
Or to use a function that counts the elements ('length', 'size', 'numel')

Sign in to comment.

More Answers (1)

function out = grader(a,b,varargin)
out = true
for ii = 1:length(varargin)
c = varargin{ii};
d = a(c);
e = b(c);
if d == e
out = true;
else
out = false
end
end
end

Categories

Asked:

Gin
on 6 Oct 2020

Commented:

on 18 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!