Not enough input argument frenzy.
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi, I have two simple functions in two files. in in opt_compute_posterior.m,
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
And in plJoint.m,
function joint = plJoint(tobs)
gamma = 2.43;
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) );
end
When I test this code by typing "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. Please lit me a light.
1 件のコメント
KSSV
2016 年 10 月 3 日
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) );
The above line from the function plJoint(), needs theta. You have to provide this value.
採用された回答
When you type
opt_compute_posterior(plJoint, 0, 300, 1000)
you are calling plJoint, but with no input arguments, thus the error. It is just as if you type this, and do not define what tobs is, do you expect this to work without error?:
plJoint() % <- no tobs
...
joint = @(theta)( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) ); % <- what is tobs?
13 件のコメント
Woongki
2016 年 10 月 3 日
I thought joint(thetavals) will return a function handle since plJoint returns a function handle. If it's wrong, how can I properly pass plJoint into opt_compute_posterior function?
The concept of passing a function handle is fine. The problem is where its variables are defined.
You create the function handle inside plJoint, but at the moment it is created the variable tobs does not exist (because you call plJoint without any input argument). You can either define the anonymous function to:
- use existing variables in the local workspace where it is created, or
- use input variables defined within the parentheses.
tobs is neither of these, thus the error. You will solve your problem when you can answer this question: where is the value of tobs specified?
Woongki
2016 年 10 月 3 日
hmmm OK. sorry for the confusion. What I expected was more like a closure in JS or Python. By typing opt_compute_posterior(plJoint, 0, 300, 1000), what I expected was that, when it reached "postvals = joint(thetavals);", it would pass "thetavals" into plJoint, and the anonymous function would calculate and return ( ( 1 ./ (theta.^(gamma + 1)) ) .* (tobs < theta) ) with the thetavals. Am I thinking wrong?
Walter Roberson
2016 年 10 月 3 日
You just need
opt_compute_posterior(@plJoint, 0, 300, 1000)
so that you are not calling p1Joint, that instead you take a handle to it and pass the handle into the routine.
@Woongki: It will pass thetavals, exactly as you expect.
But you don't specify tobs and that is the problem. That is what my answer and comments talk about. I repeat: "You will solve your problem when you can answer this question: where is the value of tobs specified?"
Woongki
2016 年 10 月 3 日
Then, I get a error "Undefined function 'sum' for input arguments of type 'function_handle'." Should I change the code?
@Walter Roberson: please check again, a bit more carefully. The function plJoint already returns a function handle. The concept is fine, just the variables need attention.
@Wonngki: yes, you need to change the code. However Walter Roberson's suggestion is not helpful because it does not resolve your problem and just makes this discussion more complicated.
I repeat: you need to think about the variable tobs: where is its value defined?
@Stephen Cobeldick I thought tobs was specified in the local scope of plJoint, so it would be accessible when the anonymous function was called, just like in the case of closures in other language. If not, can you make examples of how I can do 1 and 2 in your comment?
@Woongki: you define tobs in the list of input augments, but you never call plJoint with any input arguments, so tobs will never be defined in the workspace. So all you need to do is specify tobs when you call plJoint:
>> opt_compute_posterior(plJoint(1), 0, 300, 1000)
ans =
Columns 1 through 7
0 0.3003 0.6006 0.9009 1.2012 1.5015 1.8018
Columns 8 through 14
2.1021 2.4024 2.7027 3.003 3.3033 3.6036
...etc
This why I was telling to to think about where the value of tobs is defined. You simply never defined its value.
"I thought tobs was specified in the local scope of plJoint"
Yes, of the local workspace when the anonymous function is created, not when it is called. As I already explained, and as the documentation clearly states (I gave you a link).
"so it would be accessible when the anonymous function was called"
Nope. See the link I gave.
"just like in the case of closures in other language"
MATLAB is not other languages. Try reading the MATLAB documentation (see the link I gave)
Woongki
2016 年 10 月 3 日
Ohhhhhh, now I see the confusion. I was confused by theta and tobs, and now I see why you talked about tobs. Many thanks!!!
Stephen23
2016 年 10 月 3 日
@Woongki: I am glad to help. Please accept my answer if it helped you to resolve your question.
Woongki
2016 年 10 月 3 日
Love to you!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Functional Programming についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
