MATLAB Answers

FOR LOOP NOT WORKING

1 view (last 30 days)
Olivia Vargo
Olivia Vargo on 30 Jul 2020
Commented: Shae Morgan on 14 Aug 2020
The For loop i am trying to run is only running the 219th, or the last variable in the loop instead of running the whole loop. I cannot figure out why.
EXANGLES = -34:1:184;
EYANGLES = deg2rad(EXANGLES);
EVSUB1 = 90;
for i = 1:length(EXANGLES)
if EXANGLES(i) < 90
EVANGLE1 = EVSUB1-FT126;
elseif EXANGLES(i) == 90
EVANGLE2 = 90;
elseif EXANGLES(i) > 90
EVANGLE3 = EVSUB1+FT126;
end
end
EVSUB = [EVANGLE1, EVANGLE2,EVANGLE3];

  0 Comments

Sign in to comment.

Answers (2)

Shae Morgan
Shae Morgan on 31 Jul 2020
EXANGLES = -34:1:184;
EYANGLES = deg2rad(EXANGLES);
EVSUB1 = 90;
FT126 = 126;
for i = 1:length(EXANGLES)
if EXANGLES(i) < 90
EVSUB(i) = EVSUB1-FT126;
elseif EXANGLES(i) == 90
EVSUB(i) = 90;
elseif EXANGLES(i) > 90
EVSUB(i) = EVSUB1+FT126;
end
end
EVSUB

  1 Comment

Shae Morgan
Shae Morgan on 14 Aug 2020
If this answers your question, please accept - thanks!

Sign in to comment.


James Tursa
James Tursa on 30 Jul 2020
Index your answers. E.g.,
if EXANGLES(i) < 90
EVANGLE1(i) = EVSUB1-FT126;
elseif EXANGLES(i) == 90
EVANGLE2(i) = 90;
elseif EXANGLES(i) > 90
EVANGLE3(i) = EVSUB1+FT126;
end
and
EVSUB = [EVANGLE1(:), EVANGLE2(:), EVANGLE3(:)];

  3 Comments

Olivia Vargo
Olivia Vargo on 31 Jul 2020
when doing this there is an error:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Shae Morgan
Shae Morgan on 31 Jul 2020
Here's a hypothetical scenario to explain why this answer is liited
i=1
If EXANGLES(1) < 90, then EVANGLE1(i) will be populated with EVSUB-FT126
i=2
if EXANGLES ~<90, EVANGLE1(i) will be assigned a zero.
i=3
if EXANGLES <90, then EVANGLE1(i) will again be assigned a value
since the later portion of your EXANGLES variable are all greater than 90 - this will cause many non-assigned values to EVANGLES1 and EVANGLES2, which will not allow them to be concatenated.
James Tursa
James Tursa on 31 Jul 2020
True. I overlooked that. The unassigned values will be 0's and the lengths can be fixed up, but that begs the question what do you want for an output? Do you want three columns with lots of zeros in them (in which case we can fix up the lengths), or did you want something else?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!