My for loop isn't working
Mostrar comentarios más antiguos
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
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.
Respuestas (1)
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.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!