How to implement this kind of coding ?

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

Show us all your code.
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
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)

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

Is this what you mentioned right ?
test_cond = 0;
dn = 0;
extra_iter_limit = 3; % or 5 you adapt
c=column number ;
while test_cond < extra_iter_limit
A=[3 X 5] % one of my matrix %your calculations
test_cond = test_cond + dn;
B=A % as per my previous code
if ((B(:,c))> (A(:,c))) %you_favourite_interrupt_criteria
dn = 1;
break
end
end
No, do not have the break if you want to continue extra iterations after the condition becomes true.
I see .But then the loop is running for a long time . Thats why i was little bit confused .In this case the iteration is not stopping ,
Most of the time i see my program is showing me it's stuck on B=A in this line .
function [A,B]=test(i ,j)
test_cond = 0;
dn = 0;
extra_iter_limit = 3; % or 5 you adapt
while test_cond < extra_iter_limit
A = randi([10 20],[i j]);
% your calculations
B=A;
test_cond = test_cond + dn;
if all(A(:,end) > B(:,end))
dn = 1;
end
end
Your A and B are the same, and it never true that A(:,end) > A(:,end) so your code never sets dn = 1.
Thats what it is showing thats why in the first iteration i want A=B ,but in the next iteration when some other parameter will include with A that time i hope the A value is changing ,So from the next iteration they will compare with each other ,this is I want to get ,can you give me any idea in it ?
function [A,B]=test(i ,j)
test_cond = 0;
dn = 0;
extra_iter_limit = 3; % or 5 you adapt
first_iter = true;
while test_cond < extra_iter_limit
A = randi([10 20],[i j]);
% your calculations
if first_iter
B=A;
first_iter = false;
else
B = something else
end
test_cond = test_cond + dn;
if all(A(:,end) > B(:,end))
dn = 1;
end
end
function [selectednewparent,selectednewparent1]=test(5,2)
test_cond = 0;
dn = 0;
extra_iter_limit = 3; % or 5 you adapt
first_iter = true;
while test_cond < extra_iter_limit
[sortbcd,choose,selectednewparent]=sbcd(i,j,PS,f); % some of my calculation from here i will get the value of selectednewparent
if first_iter
selectednewparent1=selectednewparent;
first_iter = false;
else
i want to do here just keeping the value as it is jump to next iteration and that time i will get maybe another value of selectednewparent and want to compare with them
end
test_cond = test_cond + dn;
% % nexttile([2,1]
%gen=gen+1;
if all(selectednewparent(:,end) >selectednewparent1(:,end))
dn = 1;
end
end
function [] = mainfun()
iteration = 0;
old_out2 = 0;
c = 2; % column to check
while iteration < max_iteration
% increase the counter
iteration = iteration + 1;
% some parameter and their calculation
[] = subfun1();
[, out2] = subfun2();
if iteration == 1
% at the first iteration save the result
old_out2 = out2;
else
% for all other iterations, check if the new result is bigger
if sum( old_out2(:,c) ) < sum( out2(:,c) )
% if so break the loop
break
end
end
end
I was following this programming code ,so for the current problem how i can implement this programming code ?
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.
Thanks for your advice ..I tried it

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Asked:

on 4 Nov 2022

Commented:

on 9 Nov 2022

Community Treasure Hunt

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

Start Hunting!