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!