简体   繁体   English

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

[英]MATLAB: “Not enough input arguments” error

I have two simple functions in two separate files like below: 我在两个单独的文件中有两个简单的函数,如下所示:

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

When I test this code with opt_compute_posterior(plJoint, 0, 300, 1000) , I have error of "Not enough input arguments.", and I cannot find where the hell is wrong with the codes. 当我使用opt_compute_posterior(plJoint, 0, 300, 1000)测试此代码时,出现错误“输入参数不足。”,并且我找不到这些代码到底出了什么问题。 Please lit me a light. 请给我点灯。

It looks like you are trying to pass plJoint as a function handle to opt_compute_posterior . 看来您正在尝试将plJoint作为函数句柄传递给opt_compute_posterior But if you just write plJoint , MATLAB interprets that as a function call and treats it as if you had written plJoint() . 但是,如果您只是编写plJoint ,则MATLAB会将其解释为函数调用,并将其视为已编写plJoint() To indicate that you want a function handle, you need the @ symbol: 为了表明您需要函数句柄,需要使用@符号:

opt_compute_posterior(@plJoint, 0, 300, 1000)

EDIT: 编辑:

It seems I had mistaken the intent of the original code. 看来我误解了原始代码的意图。 plJoint already returns a function handle, and you did intend to call it from the command window. plJoint已经返回了一个函数句柄,您确实打算从命令窗口中调用它。 In that case, you need to pass it a value for tobs when you call it, ie 在这种情况下,您需要在调用它时为它传递tobs的值,即

opt_compute_posterior(plJoint(0.1), 0, 300, 1000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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