How to return to excute my loop again from a line lay before condition if

1 view (last 30 days)
Dear,
I'm writing my question again to see if there anyone can tell me how return to the line
(routeInsertionPoints = sort(ceil(n*rand(1,2))) )
if ( idx1==0 ) and if I==J to generate new points.
n=6;
M=6;
M = 4*ceil(M/4);
R=[1 2 1 1 2 1
2 2 1 2 2 1];
y=[2 4 5 3 6 1
2 1 6 5 3 4
4 1 3 6 5 2
3 4 1 2 5 6
6 2 3 4 1 5
1 2 3 4 5 6
5 4 2 1 3 6
4 6 3 2 5 1];
Fit=[14 11 17 12 12 10 14 14];
new1 = [];
randomOrder = randperm(M); %choose the first four index
for p = 4:4:M
rtes = y(randomOrder(p-3:p),:) % select rows from y regarding to the index of 4 elements from randomorder
maxval = Fit(randomOrder(p-3:p)); % give the Fit of these rows
[ignore,idx] = max(maxval) ; % max value between these individuals
best4 = rtes(idx,:) % give best row by writing its row from y
R=R(:,best4) %the changing in R with respect to best4
for k = 1:3 % finding new rows from the row best4
tmp(k,:) = best4;
InsertionPoints = sort(ceil(n*rand(1,2))) % we can take the indices of the blocks, they take two numbers randomly to swap
I = InsertionPoints(1);
J = InsertionPoints(2);
i1=find(R(:,I)==1)
j1=find(R(:,J)==1)
%
if size(i1,1)<size(j1,1)
i1=[i1;zeros((size(j1,1))-(size(i1,1)),1)];
idx1=ismember(j1,i1,'rows');
elseif size(i1,1)>size(j1,1)
j1=[j1;zeros((size(i1,1))-(size(j1,1)),1)];
idx1=ismember(i1,j1,'rows');
elseif size(i1,1)==size(j1,1)
idx1=ismember(i1,j1,'rows');
end
if idx1==0
% here I need to return to up to generate points again
else
tmp(k,I:J) = tmp(k,[I+1:J I])
end
end
new(p-2:p,:) = tmp;
new=[best4; tmp ];
new1=[new1; new]
end
y = new1;
size(y,1);
  2 Comments
dpb
dpb on 30 Sep 2015
Would seem would need to encapsulate that inside another while or for loop which iteration is controlled by whatever is the desired set of conditions. To get back would then be a break.
Or perhaps one could construct the logic as an error handler utilizing try...catch; it's hard to say specifically w/o a better definition of the problem than just trying to read someone's code from scratch.
Perhaps if you were to write a pseudo-code with only the outline of the process without the details the overall structure would be more apparent to readers here...as is, it's so detailed not easy to see the forest for the trees, so to speak...
nadia nadi
nadia nadi on 1 Oct 2015
Dear dpb,
this is the code in simple, sorry this is the first time I write like this, you can see I need to return back to generate points if I get I==J ,and if idx1==0, I hope you can help me with it, I need to write fast code because I'm going to call it a lot to my main code.
Thank you for any help.
Nadia

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Oct 2015
You cannot return to an earlier line in code. You need to put your code in a loop, and when you want to return to the earlier code, execute a "continue" statement. Be sure that there is a way to exit the loop.
For example,
....
while true
this is the line you want to repeat
...
if conditions are right to repeat the code
continue;
end
....
break; %if you do not want to repeat the code
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!