How to implement more steps in mathwork coding?

1 回表示 (過去 30 日間)
Akash Pal
Akash Pal 2022 年 11 月 11 日
コメント済み: Akash Pal 2022 年 11 月 16 日
fun [ …… 0ut2 …. ]= mainfun [………………….]
iteration=0
while iteration<maxiterationno
some parameter and their calculation
iteration=iteration+1
[ ]=subfun1 [];
[ ,out2 ]= subfun2[];
if iteration == 1
% at the first iteration save the result
B = out2;
else
% for all other iterations, check if the new result is bigger
if sum( B(:,c) ) < sum( out2(:,c) )
% if so break the loop
break
end
end
Newoutput=out2(1:10,:)
end
end
By doing this I am getting my generation number with my expected value ,but i want to do another further steps like ,
when my previous condition match that time i want to keep the value of the out2 and want to do another few iteration and everytime i want to compare with the new iteration value with the out2 value .
like maybe i got the expected out2 value at iteration number 100 , so i will keep the value of out2 as constant and will do the next few iteration and everytime i will compare the value with different iteration means 101th iteration with 100 iteration or 102 iteration value with 100 iteration value where out2 value will be constant after 100th iteration when the first condition will match and then it will continue maybe another 3 to 5 iteration .
then i want to stop my iteration .
Thank you in advance .

採用された回答

Image Analyst
Image Analyst 2022 年 11 月 14 日
編集済み: Image Analyst 2022 年 11 月 14 日
@Akash Pal are you sure you're incrementing iteration immediately upon entering the while? And if you've extended the number of iterations, make sure you don't extend it again.
function [ …… out2 ]= mainfun […………………]
iteration=0;
maxiterationno = 1000;
alreadyExtended = false;
while iteration < maxiterationno
iteration = iteration + 1; % ADD THIS LINE!
if iteration == 1 % At the first iteration save the result
B = out2;
else % For all other iterations, check if the new result is bigger
if sum( B(:,c) ) < sum( out2(:,c) )
% break - no, don't break the loop, but:
% Tell it to continue for another 5 iterations.
if ~alreadyExtended % ADD THIS LINE!
% If we have not yet extended it 5 iterations yet, do so now.
maxiterationno = iteration + 5;
alreadyExtended = true; % Flag it so we don't keep extending the max iteration number.
end
B = out2;
end
end
end
  2 件のコメント
Akash Pal
Akash Pal 2022 年 11 月 15 日
yes
Image Analyst
Image Analyst 2022 年 11 月 15 日
OK, since this prevents it from extending the maximum number of iterations forever, and lets you continue the loop 5 beyond the normal stopping point, could you click the "Accept this answer" link? Thanks in advance. 🙂

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2022 年 11 月 11 日
編集済み: Jan 2022 年 11 月 11 日
Maybe:
fun [ …… 0ut2 …. ]= mainfun [………………….]
iteration=0
while iteration < maxiterationno
...
if iteration == 1 % at the first iteration save the result
B = out2;
else % for all other iterations, check if the new result is bigger
if sum( B(:,c) ) < sum( out2(:,c) )
% break - no, don't break the loop, but:
maxiterationno = iteration + 5;
B = out2;
end
end
end
  7 件のコメント
Jan
Jan 2022 年 11 月 15 日
Yes. Exactly this is done by my code, isn't it?
Akash Pal
Akash Pal 2022 年 11 月 16 日
Yes ,the code is also do the same ,but in my case i got some error when i am trying to change some parameter .Thank you for your help to get the idea how to implement it .

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by