Info
This question is closed. Reopen it to edit or answer.
Having trouble writing conditional statements?
2 views (last 30 days)
Show older comments
So ive been tasked with finding total distance traveled in x and y based off of an array of azimuths and a second array of displacements.
lets say the azimuths are [0, 90, 0] and the displacements for each azimuth are [10, 10, 20]
The total (absolute value of each individual distance) distance traveled in x and y directions should be [10, 30]. I need to write a general code that can handle any azimuth between 0 and 360 degrees. I have it broken down into 8 different conditions any individual azimuths could fall into. I have 4 of these done but need help on the others.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/415173/image.jpeg)
note: theat symbol phi represents an azimuth given in the first array while d represents the displacement for said azimuth.
So far i think ive coded conditions 1,3,5, and 7 correctly but idk how to do the others. '
my code so far:
azimuths = [0, 90, 0];
lengths = [10, 10, 20];
for n = 1:length(azimuths)
if azimuths(n) == 0 %condition 1
y = lengths.*(azimuths==0);
y1 = sum(y);
if azimuths(n) > 0 && azimuths(n) < 90 %condition2
ans = find(0 < azimuths < 90);
end
if azimuths(n) == 90 %condition3
x = lengths .* (azimuths == 90);
x1 = sum(x);
end
if azimuths(n) == 180 %condition 5
y = lengths.*(azimuths == 180);
y1 = sum(y);
end
if azimuths(n) == 270 %condition7
x = lengths .* (azimuths == 270);
x1 = sum(x);
end
end
3 Comments
Setsuna Yuuki.
on 16 Nov 2020
Edited: Setsuna Yuuki.
on 16 Nov 2020
You can use:
%if()
% ...
%elseif()
% ...
%end
azimuths = [0, 90, 0];
lengths = [10, 10, 20];
for n = 1:length(azimuths)
if azimuths(n) == 0 %condition 1
y = lengths.*(azimuths==0);
y1 = sum(y);
elseif azimuths(n) > 0 && azimuths(n) < 90 %condition2
ans = find(0 < azimuths < 90);
elseif azimuths(n) == 90 %condition3
x = lengths .* (azimuths == 90);
x1 = sum(x);
elseif azimuths(n) == 180 %condition 5
y = lengths.*(azimuths == 180);
y1 = sum(y);
elseif azimuths(n) == 270 %condition7
x = lengths .* (azimuths == 270);
x1 = sum(x);
end
end
But your conditional statements are correct
Answers (1)
Sulaymon Eshkabilov
on 16 Nov 2020
Hi,
Here are the "if" conditions:
if azimuths(n)> 90 && azimuths(n)<180 %condition 4
...
end
...
if azimuths(n)> 180 && azimuths(n)<270 %condition 6
...
end
...
if azimuths(n)> 270 && azimuths(n)<360 %condition 8
...
end
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!