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)))
source
share