problem in my code

1 view (last 30 days)
ra
ra on 6 Aug 2013
Reopened: Walter Roberson on 22 Dec 2018
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

Answers (2)

Sathish Kumar
Sathish Kumar on 6 Aug 2013
>>>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).

Jan
Jan on 6 Aug 2013
Edited: Jan on 6 Aug 2013
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 Comments
ra
ra on 6 Aug 2013
I thank you for answering. Actually , the function's name is K=retrosample4(n,alpha). I'd like to have N1=2=max(K) if I get K=[2 2 2 2 2] with the example below. I set k in the if and in the else ( after this k, I shew p to recognize it).
It would be ok if I could go back to the for after each else without generating a new u(i). Could you help me for this please ? Thanks.
retrosample3(5,2)
u =
0.1213 0 0 0 0
k =
1
p =
0.7730 0.1530
u =
0.6244 0 0 0 0
goes back to while after the else
k =
1
p =
0.7730 0.1530 0.0238
k =
2
u =
0.6244 0.4831 0 0 0
k =
1
p =
0.7730 0.1530 0.0238 0.0216
k =
2
u =
0.6244 0.4831 0.4186 0 0
k =
1
p =
0.7730 0.1530 0.0238 0.0216 0.0044
k =
2
u =
0.6244 0.4831 0.4186 0.4680 0
k =
1
p =
0.7730 0.1530 0.0238 0.0216 0.0044 0.0100
k =
2
u =
0.6244 0.4831 0.4186 0.4680 0.6795
k =
1
p =
0.7730 0.1530 0.0238 0.0216 0.0044 0.0100 0.0038
k =
2
N1 =
7
ans =
2 2 2 2 2
Jan
Jan on 6 Aug 2013
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!