problem in my code
1 次查看(过去 30 天)
显示 更早的评论
i=1;
N1=1;
V= zeros(1,N1);
V(1)=betarnd(1,alpha);
u=zeros(1,n);
K=zeros(1,n);
X=zeros(1,n);
Z=zeros(1,N1);
Z(1)= priorpara(4,5,6,5); % create a probabilty sampling from normal-gamma(no % probleme with this)
p=zeros(1,N1);
p(1)=V(1);
%----------------------------------------------------------
while i<=n
u(i)=rand(1,1);
for k=1:N1
if sum(p(1:k-1))<u(i)<= sum(p(1:k))
K(i)=k; %individu i rangé dans la kieme boite (N1 boites)
X(i)=Z(k); % allocation du ieme individu à la kieme composante de P~DP(alpha, Htheta=priorpara)
i=i+1;
break;
else
N1=N1+1;
B=1-V;
C=cumprod(B);
V(N1)= betarnd(1,alpha);
p(N1)=C(N1-1).*V(N1);
Z(N1)=priorpara(4,5,6,5);
% Here I'd like to go back to the for loop but it goes back to the while !
end
end
end
0 个评论
回答(2 个)
Sathish Kumar
2013-8-6
>>>Here I'd like to go back to the for loop but it goes back to the while !
That is because the loop runs from k=1:N1 and you have defined your N1=1. So the loop runs only once and exits and go to upper loop(the WHILE loop).
0 个评论
Jan
2013-8-6
编辑:Jan
2013-8-6
The program reaches the WHILE loop at the shown location only, when the FOR is finished. And then going back to the FOR loop is meaningless.
I suggest to set some breakpoints in the code to find out, what's going on. Matlab does exactly, what the function forces it to do. So please explain with more details, what you want to change by which command.
2 个评论
Jan
2013-8-6
The question is still not clear to me. When you want to leave the FOR loop in both cases of the IF, simply set a break behind the IF block. But then the FOR loop runs once only and you can replace it by: k = 1.
Notice that it is not possible to reconsider the posted values while I'm exhausted by scrolling up and down between the code and the values. Therefore I've deleted the duplicate huge comment.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!