# How to implement this kind of coding ?

1 view (last 30 days)
Akash Pal on 4 Nov 2022
Commented: Akash Pal on 9 Nov 2022
Maybe I have a matrix called A any size . It's in a loop ,so after first iteration i got the value which i want to comapre with my previous value .
I am giving the logic like if my next iterations values are better than the previous iteration then the iteration will stop .But i want to make sure that in this stop my iteration should not stop it should run another 3 or 5 iteration and everytime compare with it's previous iteration value and stop .
while iter< max_iter
.....
.......others steps are here for the calculations puspose
iter=iter+1;
if iter ==1
B=A;
else
if ((B(:,c))> (A(:,c)))
break
end
end
....
...
end
Here c is the column number which i want to compare .
May be after 10 iteration I am getting that my next generation value is better ,but i want to continue the iteration for another 3 to 5 times and then want to make sure that everytime i am getting the better result . and also want to make sure the generation numbner with it .
Walter Roberson on 4 Nov 2022
if ((B(:,c))> (A(:,c)))
That means the same thing in MATLAB as if you had written
if all(B(:,c) > A(:,c))
Are you sure that is what you want?

Bjorn Gustavsson on 4 Nov 2022
You while-loop will continue as long as the test-condition is true. You want to continue "a couple" of iterations after your desired value has started to improve? Then make that the test-condition, perhaps something like this:
test_cond = 0;
dn = 0;
extra_iter_limit = 3; % or 5 you adapt
while test_cond < extra_iter_limit
test_cond = test_cond + dn;
if you_favourite_interrupt_criteria
dn = 1;
end
end
This loop will continue until you_favourite_interrupt_criteria then dn is increased to 1 and test_cond will start to increase at the next iteration. You might consider another interrupt-condition in case your search fails.
HTH
Akash Pal on 9 Nov 2022

### Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by