Loop iteration with condition met

조회 수: 2(최근 30일)
Johan Johan 24 Nov 2019
댓글: Johan Johan 24 Nov 2019
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 24 Nov 2019
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 24 Nov 2019
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 .

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

Community Treasure Hunt

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

Start Hunting!

Translated by