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

Hi, with your help i was able to program this code.
clc
M = dec2bin(0:2^15-1, 15);
%A=zeros(5);
j=1;
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))];
A(2,3:6,i)=[str2num(M(i,6)),str2num(M(i,7)),str2num(M(i,8)),str2num(M(i,9))];
A(3,4:6,i)=[str2num(M(i,10)),str2num(M(i,11)),str2num(M(i,12))];
A(4,5:6,i)=[str2num(M(i,13)),str2num(M(i,14))];
A(5,6,i)=[str2num(M(i,15))];
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)
B(:,:,j)=A(:,:,i)
j=j+1;
end
end
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 comentario

Jan
Jan el 3 de Jul. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Guru
Guru el 3 de Jul. de 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
pause
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
B(:,:,j)=A(:,:,i)
in your if statement.

4 comentarios

andreas
andreas el 3 de Jul. de 2013
I edited my post, so that my problem will be a bit more clear to you.
Again for clarification, this is addressed. You are mistaken that the output displayed to the command window is the same as the output of your array B. By not suppressing the output with a semicolon
B(:,:,j)=A(:,:,i);
you are confusing yourself of this. MATLAB executes its commands line by line. So in your first iteration of the for loop, it creates B(:,:,1). This means that B is currently a NxM array since we ignore the 3rd dimension which is currently dimensionality of 1. The next iteration of the for loop, it will change the size of B as it assigns B(:,:,2). However since you did not suppress the output, it will continue to output the value of B which is now a NxMx2 array, hence why you see B(:,:,1) = ... and B(:,:,2) = at the command line display.
The same behavior can be illustrated with the following code:
for n=1:10
A(n) = n^2
end
From this you will see "A = " ten different times where each time A is growing in size. The speed issues of dynamically changing your variable sizes aside, if you then type
A
at the command line you will see the final value of A which would be a 10 element vector.
In short, what you are seeing on the command line display as you execute your code is the step by step process that MATLAB is taking as it is iterating in the for loop, B is growing in size. You should suppress the output by terminating the line with a ";" and if you want to see the value of B, you can type "B" on a line after the for loop has been terminated with the "end"
andreas
andreas el 3 de Jul. de 2013
Thank you. I just didn#t expect that it would take so long.
Guru
Guru el 4 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by