Not enough input argument frenzy.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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?
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
