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 件のコメント

Stephen23
Stephen23 2020 年 10 月 6 日
編集済み: Stephen23 2020 年 10 月 6 日
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.
Gin
Gin 2020 年 10 月 6 日
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?
Gin
Gin 2020 年 10 月 6 日
Ah okay, nevermind. Thanks again!

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2020 年 10 月 6 日
編集済み: Stephen23 2020 年 10 月 6 日

0 投票

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 件のコメント

Sri sai teja Palisetti
Sri sai teja Palisetti 2020 年 11 月 19 日
Thankyou , so much stefen it helped me to submit my assignment.
Abdul Hanan Wali
Abdul Hanan Wali 2022 年 6 月 24 日
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
Abdul Hanan Wali
Abdul Hanan Wali 2022 年 6 月 24 日
somehow this code will run? I am not able to compare the functions
Aravind Gopal
Aravind Gopal 2022 年 9 月 23 日
編集済み: Aravind Gopal 2022 年 9 月 23 日
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})
Stephen23
Stephen23 2022 年 9 月 23 日
"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.
Otto Alfonso
Otto Alfonso 2024 年 7 月 18 日
'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')

サインインしてコメントする。

その他の回答 (1 件)

Rasheed Aiyedun
Rasheed Aiyedun 2022 年 8 月 30 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeDebugging and Improving Code についてさらに検索

質問済み:

Gin
2020 年 10 月 6 日

コメント済み:

2024 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by