问题
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
回答1:
Normalize the difference, abs operation is not necessary because mod(x,y) takes the sign of y.
normDeg = mod(a-b,360);
This will be a number between 0-360, but we want the smallest angle which is between 0-180. Easiest way to get this is
absDiffDeg = min(360-normDeg, normDeg);
回答2:
When doing math with angles, it is useful to normalize them first. This function normalizes all angles to a (-180,180] range:
normalizeDeg=@(x)(-mod(-x+180,360)+180)
Now having this function to normalize, the absolute difference can be calculated:
absDiffDeg=@(a,b)abs(normalizeDeg(normalizeDeg(a)-normalizeDeg(b)))
回答3:
How about unsing unwrap ? Here is a try:
absDiffDeg = @(a,b) abs(diff(unwrap([a,b]/180*pi)*180/pi));
Best,
来源:https://stackoverflow.com/questions/32276369/calculating-absolute-differences-between-two-angles