My for loop isn't working
Show older comments
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 Comment
Stephen23
on 12 Jan 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.
Answers (1)
James Tursa
on 12 Jan 2017
1 vote
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).
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!