gpt4 book ai didi

MATLAB 奇数 'Too many input arguments' 错误

转载 作者:行者123 更新时间:2023-12-01 14:38:45 30 4
gpt4 key购买 nike

对于一个项目,我尝试使用 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 也不会改变任何内容。所以我的问题是,为什么会发生这种情况以及如何解决它?

最佳答案

尝试使用

MYNAME_sa(2, 10000, @eval_square)

关于MATLAB 奇数 'Too many input arguments' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636391/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com