How to implement this kind of coding ?

1 visualizzazione (ultimi 30 giorni)
Akash Pal
Akash Pal il 4 Nov 2022
Commentato: Akash Pal il 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 Commenti
Akash Pal
Akash Pal il 4 Nov 2022
iter=iter+1;
if iter ==1
B=A;
else
if ((B(:,c))> (A(:,c)))
break
end
end
We can think only this coding ,where i want to compare the iterations value for a specific column but also i want to do next 3 to 5 iteration and then want to stop my iteration when i will get the expected result
Walter Roberson
Walter Roberson il 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?

Accedi per commentare.

Risposte (1)

Bjorn Gustavsson
Bjorn Gustavsson il 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 Commenti
Bjorn Gustavsson
Bjorn Gustavsson il 9 Nov 2022
One general advice trying to figure out how an algorithm work is to simplify the inputs to such a small size that you can run the function by hand. Then you set the debugger to:
>> dbstop in test
or just activate the debugger in the editor.
After that call the function with your small example, then matlab will let you step through the function line by line - and you can inspect what happens and compare to what you want/need to happen. That sould let you understand the general behaviour of your function. Then you might still need to figure out edge-cases, but that might be for later.
Akash Pal
Akash Pal il 9 Nov 2022
Thanks for your advice ..I tried it

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by