Calculation of absolute differences between two angles

I have two angles a and b, I want to calculate the absolute difference between both angles. Examples

>> absDiffDeg(360,5)
ans = 5
>> absDiffDeg(-5,5)
ans = 10
>> absDiffDeg(5,-5)
ans = 10
+4
source share
3 answers

To normalize the difference, the operation abs is not needed, because mod (x, y) takes the sign y.

normDeg = mod(a-b,360);

It will be a number between 0-360, but we need the smallest angle between 0-180. The easiest way to get this is

absDiffDeg = min(360-normDeg, normDeg);
+4
source

When doing math with angles, it is useful to normalize them first. This function normalizes all angles to the range (-180,180):

 normalizeDeg=@(x)(-mod(-x+180,360)+180)

Now that this function is normalized, you can calculate the absolute difference:

absDiffDeg=@(a,b)abs(normalizeDeg(normalizeDeg(a)-normalizeDeg(b)))
+2
source

? :

absDiffDeg = @(a,b) abs(diff(unwrap([a,b]/180*pi)*180/pi));

,

+2

All Articles