[英]MATLAB: “Not enough input arguments” error
我在两个单独的文件中有两个简单的函数,如下所示:
function [thetavals postvals] = opt_compute_posterior(joint, theta_min, theta_max, num_steps)
thetavals = linspace(theta_min, theta_max, num_steps);
postvals = joint(thetavals);
postvals = postvals / ( sum(postvals) .* ( (theta_max - theta_min)/num_steps ));
end
function joint = plJoint(tobs)
gamma = 2.43;
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) );
end
当我使用opt_compute_posterior(plJoint, 0, 300, 1000)
测试此代码时,出现错误“输入参数不足。”,并且我找不到这些代码到底出了什么问题。 请给我点灯。
看来您正在尝试将plJoint
作为函数句柄传递给opt_compute_posterior
。 但是,如果您只是编写plJoint
,则MATLAB会将其解释为函数调用,并将其视为已编写plJoint()
。 为了表明您需要函数句柄,需要使用@
符号:
opt_compute_posterior(@plJoint, 0, 300, 1000)
编辑:
看来我误解了原始代码的意图。 plJoint
已经返回了一个函数句柄,您确实打算从命令窗口中调用它。 在这种情况下,您需要在调用它时为它传递tobs
的值,即
opt_compute_posterior(plJoint(0.1), 0, 300, 1000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.