How to stop my "if" statement in a "for" loop

5 vues (au cours des 30 derniers jours)
andreas le 3 Juil 2013
Hi, with your help i was able to program this code.
M = dec2bin(0:2^15-1, 15);
for i=1:2^15
A(1,2:6,i) =[str2num(M(i,1)),str2num(M(i,2)),str2num(M(i,3)),str2num(M(i,4)),str2num(M(i,5))];
if (A(1,2,i)==1) & (A(1,3,i)==1) & (A(1,4,i)==1) & (A(1,5,i)==1) & (A(2,3,i)==1) & (A(2,6,i)==1)
For some reason it doesn't stop after creating all the B(:,:,j) and starts with j=1 again. After creating B(:,:;501) he starts with B(:,:,1) again.
PS: If i kill the the program after the program already repeated to create the B(:,:,j) at some point, i can see that it didn't create the last few allowed matrices. Whats the reason for that?
  1 commentaire
Jan le 3 Juil 2013
Did you see our suggestions if your other questions? The code can be compressed significantly and the result would be tremendously faster and nicer. It discourages me to post further asnwers, when I see that the former ones are ignored.
You can use the debugger to find the problems locally: Set a breakpoint in a line, which reveals the problems.

Connectez-vous pour commenter.

Réponse acceptée

Guru le 3 Juil 2013
Not clear on your question exactly, but:
Q: How to stop an if statement in a for loop? A: Either make the if statement false or true accordingly, or comment it out
Q: How do I pause execution within an if statement in a for loop? A: Use the command
within your if statement
Q: If I kill the program at some point, such as CTRL+C, I end up missing data. Why? A: When you kill the code from running it stops immediately and anything not finished will stop. If you don't want to see the output displayed for the B matrix, which you don't need to have it as it slows down the execution and the data is stored in the workspace where you can view it at your own leisure. You can accomplish this by adding a semi-colon (;) at the end of the line
in your if statement.
  4 commentaires
Guru le 4 Juil 2013
As fast as computers have become, anything graphical related is always slower. The slowdown is simply a side-effect that your code is waiting for your graphics card to update appropriately before it can finish.

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by