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 Commento

@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.

Accedi per commentare.

Risposte (1)

James Tursa
James Tursa il 12 Gen 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).

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 12 Gen 2017

Commentato:

il 12 Gen 2017

Community Treasure Hunt

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

Start Hunting!

Translated by