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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by