Loop iteration with condition met

조회 수: 1 (최근 30일)
Johan Johan
Johan Johan 2019년 11월 24일
댓글: Johan Johan 2019년 11월 24일
I have this program in matlab
close
clc
%%%%%%%%%%%%%%%%
Tol = 0.5;ma=10;t=[1 2];B=[];loopEnd=3; myData = zeros(ma,loopEnd);
%while loop execution
for a=1:ma
x=rand()
v=sum(x*t);
if v<Tol
return
end
B=[B,v]
end
How can i add condition (if statment) ,the condition is :
1-if the result is big than 1.5 cancel it and biging new iteration,
2-if the result is less than 0.5 save it in B, if not repeat new iteration (new loop) ,the loop is repeat until the final vector have 10 values.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 24일
Tol=1.5;ma=10;
t=[1 2];
B=[];
for a=1:ma
x=rand();
result=sum(x*t);
while result>Tol || result<0.5
x=rand()
result=sum(x*t);
B(a)=result;
end
end
B
  댓글 수: 1
Johan Johan
Johan Johan 2019년 11월 24일
Thank you,but i have other question,If i want sub loop for example ma=5 if the condition is not met i cancel the loop and starting new loop.
Why , if using optimization method, sometimes the first iterations such as 10 iterations don't met the better solution .

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

추가 답변 (0개)

카테고리

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