How to implement this kind of coding ?

1 view (last 30 days)
Akash Pal
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 .
  3 Comments
Walter Roberson
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?

Sign in to comment.

Answers (1)

Bjorn Gustavsson
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
% your calculations
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
  10 Comments
Akash Pal
Akash Pal on 9 Nov 2022
Thanks for your advice ..I tried it

Sign in to comment.

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