(HOW) I need the code run again if the ( else condition return ) till give me value

70 次查看(过去 30 天)
Hi all. I have my code, and if the "if" condition is false for any value of i or j then it should stop and do the function again until the if condition true for all the values for i and j.
After else there is a return but it stops running and I run it again to get the (bestsol value) which do (Do this command line) and for loop again from the beginning. I need this code to run until I get (bestsol value) without it returning early.
I have used while true but it keeps running. Also I used while Halt==1 but work as no while loop.
function Labelvertices
%DO THIS LINES
%-----
%-----
%----
%-----Do this command line-------
verticeslabe=[0,randi(7)];
makematrix % it is function give us the matix d
for i= 1:7
for j=1:7
if (i~=j)
if % codition 1 met || codition 2 met
verticeslabe(i)= % do this;
verticeslabe(j)= % do this;
else
% does not meet codition 1 OR codition 2 go return to (do this command line) and do for loop again till we have (bestsol)
return ;
end
end
end
end
bestsol=verticeslabe(:,1:7) % this values i need it
end
  1 个评论
Rik
Rik 2021-9-27
Return will exit the function, not just a loop.
Your code is too compact now. E.g. makematrix is either not a function, or you're not storing the result.
Perhaps you want to make your nested loop a subfunction that returns a second output: a valid flag. That way you can use a while loop and repeatedly call this until the flag is true.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-9-27
编辑:Image Analyst 2021-9-28
Try this when you define it:
function success = Labelvertices
success = true;
% existing code....
% Then inside the else where it fails, set success equal to false. New code:
success = false;
return;
Now when you call it use a while loop, use code like this:
success = false
loopCounter = 1;
maxIterations = 100; % Failsafe - way more than you think it should ever take.
while ~success && loopCounter <= maxIterations
success = Labelvertices;
if success
% It worked so we can quit now.
break;
end
loopCounter = loopCounter + 1;
end
% Alert user if it never succeeded.
if loopCounter >= maxIterations
warningMessage = sprintf('WARNING: the process did not succeed after %d attempts!', loopCounter - 1);
uiwait(warndlg(warningMessage));
end

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-9-27
do_again = true;
while do_again
do_again = false;
for i= 1:7
for j=1:7
if (i~=j)
if % codition 1 met || codition 2 met
verticeslabe(i)= % do this;
verticeslabe(j)= % do this;
else
do_again = true;
break
end
end
end
if do_again; break; end
end
end
If the else is never executed, then do_again will not be set back to false, and the while loop will finish.
  2 个评论
Walter Roberson
Walter Roberson 2021-9-28
The user requirements are clear, 'if the "if" condition is false for any value of i or j then it should stop and do the function again until the if condition true for all the values for i and j' . Stopping the function any time before the condition is true for all members of the array is contrary to the requirements to keep going until the condition is true for all the values for i and j -- even if that takes thousands of years.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by