How do I find the minimum angle difference between two angles A & B, for example:
if A = -175, B = 175 -> difference would be 10
if A = 355, B = 5 -> difference would be 10
if A = 720, B = 360 -> difference would be 0
etc.
All I know is that I have to use the absolute and modulus functions somewhere
Would appreciate any help.

 채택된 답변

Basil C.
Basil C. 2021년 3월 11일

0 개 추천

Read about principal angles...
For the 1st case:
A=-175 whose principal angle is 185 degree
A - B = 185 - 175 = 10 degrees
You can similarly solve the for the remaining cases

댓글 수: 3

Shikhar Singh
Shikhar Singh 2021년 3월 11일
Here is how they've done it in java:
public static double getDifference(double b1, double b2) {
double r = (b2 - b1) % 360.0;
if (r < -180.0)
r += 360.0;
if (r >= 180.0)
r -= 360.0;
return r;
}
System.out.println("Input in -180 to +180 range");
System.out.println(getDifference(20.0, 45.0));
I was wondering if something similar could be done on matlab
Basil C.
Basil C. 2021년 3월 11일
The problem with your code is that it A and B must be in the principal form for the code to work.
To solve your question, A and B need to be converted to the principal angle and then just subtract them
Shikhar Singh
Shikhar Singh 2021년 3월 11일
How would this work for an angle like 1060 then?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Basil C.
Basil C. 2021년 3월 12일
편집: Basil C. 2021년 3월 12일

0 개 추천

To convert any angle to its principal angle
function y = prin_angle(x)
y=rem(x,360); % returns the remainder when dividing by 360
if y<0
y=y+360;
end
end
Just pass both angles to above function individually to obtain the principal angle, then subtract the two outputs
OR
Just use the following code and pass both A and B directly:
function y = get_difference(A,B)
y=rem(B-A,360); % returns the remainder when dividing by 360
if y<-180
y=y+360;
elseif y>180
y=y-360;
end
end
P.S.: I have just converted the java code from above into matlab code

댓글 수: 5

Shikhar Singh
Shikhar Singh 2021년 3월 12일
Hey,
Thanks for converting the java code to matlab above, it works great, however there's one error that's coming up, when I input:
get_difference(-175,175) it returns -10
but when I input
get_difference(175,-175) it returns 10
I'd like for it to return 10 in both cases, how can i fix that, so that this problem doesn't occur in other cases?
function y = get_difference(A,B)
y=rem(B-A,360); % returns the remainder when dividing by 360
if y<-180
y=y+360;
elseif y>180
y=y-360;
end
y=abs(y)
end
Shikhar Singh
Shikhar Singh 2021년 3월 13일
That works perfectly now, thanks a lot!
darova
darova 2021년 3월 13일
cem polat
cem polat 2022년 1월 20일
Use anglediff function in Communications or Robotics toolbox. Since anglediff uses radians, you can use a function like below for the angle diff in degrees:
function result = angdiffdeg(from, to)
result = rad2deg(angdiff(deg2rad(from),deg2rad(to)));
end

댓글을 달려면 로그인하십시오.

제품

질문:

2021년 3월 11일

댓글:

2022년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by