How to change angles to 0 to 180

I have an array of angles ranging from [-180, 180]
please I want to change to [0, 180], how can I do this?
I have tried this code below but it's giving me [90, 180]
Angles180 = @(a) rem(180+a, 360)-90;
Result = Angles180([-90, 0, 90])
please how can I do this?

 Accepted Answer

Hi @TTA
Are you looking for the conversion like this?
Angles180 = @(a) a/2 + 90;
Result1 = Angles180([-180, 0, 180])
Result1 = 1×3
0 90 180
Result2 = Angles180([-90, 0, 90])
Result2 = 1×3
45 90 135

7 Comments

Please how about putting all the angles in the first quadrant [0, 90]?
I thought you wanted the equivalent angle of a line segment at a negative angle, like a line at -45 is the same as a line at 135 degrees, but I guess not since you didn't like my answer below. If you simply want to scale your numbers into a specified range you can use rescale, like
angles = rescale(angles, 0, 90);
Of course this will change the angles of anything lying along that angle, but that seems to be what you want for some reason. The minimum original angle will be mapped to zero and the max will go to 90, and angles in between will be linearly scaled.
TTA
TTA on 29 May 2023
Edited: TTA on 29 May 2023
Honestly, the answer I accepted didn't do what I expected either. I am looking for a btter answer to accept.
What I wanted to do is I want to put all the following angles in the first quadrant ([0,90] so the ones that are already within 0 t 90 does not change and the ones that are with negative can taken as absolute.
30.9334909952858
3.52761847675680
4.59390438411553
0.673955187737078
-1.64646093428910
-5.88284857785972
-7.63488379965276
-13.2174529305943
-18.5463116650289
-22.0385436652521
-22.2420277734567
-17.4941779611427
-8.56583948331003
-0.252059075009702
5.98003127980421
10.1438497196693
3.20791262170253
-7.02580499273734
-16.6977290939165
-20.5936340375392
-21.3959894853361
-17.6311801870524
-9.70194857048854
11.7676931872348
43.0540344675305
72.0416817854922
89.0448914429762
95.9922408810587
100.875499683777
103.330610504292
104.413850582525
103.364277947976
100.617226400410
96.4283763466032
91.8382501208323
85.8745260293066
81.0699629969629
75.3550207618278
69.3012945930419
5.02597534734592
3.01291530518864
1.66779490750663
-0.0732445094082720
1.06958022532632
3.10405918566257
6.9150301883856
The problem is still unclear. You have angles that extend beyond [-90 90] and should somehow (either linearly or nonlinearly) map to [0 90]. Where should the following angles end up?
[-120 -60 -30 30 60 120]
I'm just trying to see if I put all the to 0 to 90 degrees without affecting those that are already within 0 t0 90 deg.
DGM
DGM on 29 May 2023
Edited: DGM on 29 May 2023
So then should the answer be
[0 0 0 30 60 90]
or
[90 60 30 30 60 90]
or
[60 30 60 30 60 30]
or something else?
Thanks for your effort

Sign in to comment.

More Answers (2)

How about just adding 180 to all angles less than 0, so for example -135 becomes +45 degrees.
mask = angles < 180;
angles(mask) = angles(mask) + 180; % Only add 180 to negative angles.

2 Comments

angles = mod(angles,180);
TTA
TTA on 30 May 2023
Edited: TTA on 30 May 2023
@DGM you are right.
for example, To find the equivalent angle within the range of 0 to 90 degrees for -20 degrees, you can use the modulo operation and add multiples of 90 until the angle falls within the desired range.
Here's how you can calculate the equivalent angle:
  1. Take -20 modulo 360: -20 % 360 = 340. This step ensures the angle is within the range of 0 to 360 degrees.
  2. Since 340 is greater than 90, subtract multiples of 90 to bring it within the range of 0 to 90 degrees.
  • 340 - 270 = 70
Thanks

Sign in to comment.

You say "What I wanted to do is I want to put all the following angles in the first quadrant ([0,90] so the ones that are already within 0 t 90 does not change and the ones that are with negative can taken as absolute."
Well, what about this:
angles = abs(angles);
???

Categories

Find more on Programming in Help Center and File Exchange

Asked:

TTA
on 29 May 2023

Commented:

TTA
on 30 May 2023

Community Treasure Hunt

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

Start Hunting!