필터 지우기
필터 지우기

Regarding Iteration

조회 수: 2 (최근 30일)
Muruganandham Subramanian
Muruganandham Subramanian 2012년 5월 4일
Hi all,
for j=1:30
x(:,j) = unifrnd(minsvc(j), maxsvc(j), no_pop,1);
if busdata(j,2)==0
rchoices = size(x(:,j));
randchoice = x(rchoices(1),j);
Qg(j,17)=randchoice;
else
Qg(j,17)=busdata(j,17);
end
v(:,j)=unifrnd(minv(j), maxv(j),no_pop,1);
end
Here, in this if the busdata(j,2)==0 condition is satisfied, i need to execute that if for only once that too for random 'j' values. Is there any solution for this?
  댓글 수: 1
Jan
Jan 2012년 5월 4일
I do not understand the question.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2012년 5월 4일
Try adding a break statement:
for j=1:30
x(:,j) = unifrnd(minsvc(j), maxsvc(j), no_pop,1);
if busdata(j,2)==0
rchoices = size(x(:,j));
randchoice = x(rchoices(1),j);
Qg(j,17)=randchoice;
break; % Exit the for loop.
else
Qg(j,17)=busdata(j,17);
end
v(:,j)=unifrnd(minv(j), maxv(j),no_pop,1);
end
However, if j = 42 when the condition becomes true and it breaks, then the for loop has already executed 42 times before it exits, so in that regard, like Jan, I'm a bit confused.
  댓글 수: 2
Muruganandham Subramanian
Muruganandham Subramanian 2012년 5월 4일
If, we give break statement, it will totally come out from the for loop,but i have changed into like this.
for j=1:no_dim
x(:,j)=unifrnd(minsvc(j), maxsvc(j),no_pop,1);
ii=1;
while ii<=no_dim && ii==1
a=1;
b=no_dim;
ij= a + (b-a).*rand(1);
ii=round(ij);
if busdata(j,1)==ii && busdata(ii,2)==0
fprintf('The SVC device is added in: %gth bus\n\n',ii);
rchoices = size(x(:,ii));
randchoice = x(rchoices(1),ii);
fprintf('The optimized SVC rating is: %g Mvar\n\n',randchoice);
Qg(ii,17)=randchoice;
else
Qg(j,17)=busdata(j,17);
end
end
v(:,j)=unifrnd(minv(j), maxv(j),no_pop,1);
% ii=ii+1;
end;
but it still have a problem. for 'ii' it should execute only once. did you understand what exactly my problem is?
Jan
Jan 2012년 5월 4일
I still do not understand the problem.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by