How to implement this kind of coding ?
1 view (last 30 days)
Show older comments
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
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?
Answers (1)
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
Bjorn Gustavsson
on 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.
See Also
Categories
Find more on Logical 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!