Calculating absolute differences between two angles

泄露秘密 提交于 2019-12-13 13:13:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!