繁体   English   中英

MATLAB:“输入参数不足”错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM