对于一个项目,我正在尝试使用matlab在另一个.m文件中调用一个函数。但是,它说“没有足够的输入参数”,即使我确实通过了我相当确定的足够的输入参数。
在eval_square.m中:
function f = eval_square(x)
% fitness function of the magic square
%
% Parameters
% ----------
% x : array, the solution vector that represents a magic square.
% By default, the solution vector is converted to a magic square
% columnwisely.
% Output
% ----------
% f : double, the error value of the input solution vector.
% the mean squared error (MSE) of all each row, column and
% diagonal sum to the magic constant is computed
%
n = sqrt(length(x));
%More stuff, but error occurs at this line.
在MYNAME_sa.m中:
function [xopt, fopt] = MYNAME_sa(dim, eval_budget, fitness_func)
%Stuff
if dim == 2
len = 12^2; % length of the solution vector, shoud be 12^2
% when dim == 2
elseif dim == 3
len = 7^3; % length of the solution vector, shoud be 7^3 when
% dim == 3
end
%Stuff
s = randperm(len)
f = fitness_func(s)
%More stuff.
它应该评估长度为12 ^ 2的随机排列作为一个魔方,看它与最优的接近程度(即它与实际魔方的距离有多近),理论上它与魔方的相同(eval_cube),但发生同样的错误。
有问题的错误:
>> MYNAME_sa(2, 10000, eval_square)
Error using eval_square (line 18)
Not enough input arguments.
Note that line 18 is n = sqrt(length(x));
如果我将eval_square硬编码到函数中并不重要 - 它似乎理解我想调用eval_square就好了,但它只是没有传递s或什么?我不明白为什么。我也试过硬编码n到12,但这也不起作用,当我尝试实际使用x时错误弹出。将fitness_func更改为@fitness_func也不会改变任何内容。 所以我的问题是,为什么会发生这种情况,我该如何解决?
答案 0 :(得分:5)
尝试
MYNAME_sa(2, 10000, @eval_square)