How to implement more steps in mathwork coding?

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 .

 Akzeptierte Antwort

Image Analyst
Image Analyst am 14 Nov. 2022
Bearbeitet: Image Analyst am 14 Nov. 2022

0 Stimmen

@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 Kommentare

Akash Pal
Akash Pal am 15 Nov. 2022
yes
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. 🙂

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 11 Nov. 2022
Bearbeitet: Jan am 11 Nov. 2022

0 Stimmen

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 Kommentare

Akash Pal
Akash Pal am 14 Nov. 2022
yes ,it's working but here my iteration number is showing 1000,because i fixed my maxiterationno=1000 ,but i want the iteration number where my loop is ending and i am getting the expected result .
Jan
Jan am 14 Nov. 2022
"but i want the iteration number where my loop is ending and i am getting the expected result" - This does not contain enough information to suggest a solution.
If the wanted ondition sum(B(:,c)) < sum(out2(:,c)) is met, the number of iterations should not reach 1000 anymore, because this code limits maxiterationno to iteration+5.
Akash Pal
Akash Pal am 14 Nov. 2022
Bearbeitet: Akash Pal am 14 Nov. 2022
when i simply mentioning the break in the loop it is giving me the iteration number maybe that is sometime 100 or any other value which is less than the maxinterationno .
but if i don't mention any break ,then that time it simply showing the iteration is 1000 which is same as my maxiterationno.
(B(:,c)) < (out2(:,c))
here if i want to check that my selected columns all value are better then it will stop ,Sorry for my mistake i have mentioned the sum ,
I only want to compare the values of the mentioned column for both the iteration .
when i am removing the sum from my condition that time is it showing maxiterationno 1000.
Jan
Jan am 14 Nov. 2022
As far as I understand, you explain the behaviour of the posted function.
Then what do you want to change?
Akash Pal
Akash Pal am 15 Nov. 2022
I want to compare the values of the column for two different matrix where my every value for previous iteration will be better than my next iteration value ,and then stop the iteration and also continue for the next 5 iteration to check that the previous iteration value is always better .
Jan
Jan am 15 Nov. 2022
Yes. Exactly this is done by my code, isn't it?
Akash Pal
Akash Pal am 16 Nov. 2022
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 .

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by