My for loop isn't working

dim_Bx = 377;
dim_By = 494;
for deg = 1:180
for img_line_B = 1:1000
Co_Bx(img_line_B,1) = img_line_B * cosd(deg) + (dim_Bx/2);
Co_By(img_line_B,2) = img_line_B * sind(deg) + (dim_By/2);
end
Ref_Bx = round(Co_Bx(:,1));
Ref_By = round(Co_By(:,2));
end
Why does Co_By constantly give me 247??
Thanks!!

1 comentario

Stephen23
Stephen23 el 12 de En. de 2017
@Brian Cheung: your code is very badly formatted. If you used consistent formatting (e.g. the MATLAB default) then your problem would be a bit easier to identify:
dim_Bx = 377;
dim_By = 494;
for deg = 1:180
for img_line_B = 1:1000
Co_Bx(img_line_B,1) = img_line_B * cosd(deg) + (dim_Bx/2);
Co_By(img_line_B,2) = img_line_B * sind(deg) + (dim_By/2);
end
Ref_Bx = round(Co_Bx(:,1));
Ref_By = round(Co_By(:,2));
end
Now you can clearly see that you have two loop. Think about what the outside loop is doing.

Iniciar sesión para comentar.

Respuestas (1)

James Tursa
James Tursa el 12 de En. de 2017

1 voto

Because you are constantly overwriting the value assigned to Co_By(img_line_B,2) with a new value because all of this is inside the for deg=1:180 loop. So only the last value sticks, and that last value used deg=180, so sind(180)=0 and the only thing left to assign is the dim_By/2 part (=247).

Categorías

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

Etiquetas

Preguntada:

el 12 de En. de 2017

Comentada:

el 12 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by