Loop iteration with condition met

3 次查看(过去 30 天)
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