How to subtract wind direction between 0-359 degrees?
조회 수: 6 (최근 30일)
이전 댓글 표시
Say I have two wind sensor measuring wind speed and direction. For speed, I can easily subtract sensor A from sensor B to determine how far apart they are. However, wind direction I find tricky because the “scale” wraps around when 359 goes to 360 (also 0 degrees). If sensor A gets a wind direction of 10 degrees and sensor B measures 350 degrees, they’re really only apart by 20 degrees. A simple subtraction would’ve called them apart by 340 degrees. How can I communicate to Matlab to perform a comparison of wind direction with this kind of compass scale?
댓글 수: 0
채택된 답변
Adam Danz
2020년 8월 26일
편집: Adam Danz
2020년 8월 28일
If you're frequently working with circular value, I recommend using the Circular Statistics Toolbox from the file exchange.
For example, the distance between 15 deg and 350 deg is
>> rad2deg(circ_dist(deg2rad(15), deg2rad(350)))
ans =
25
>> rad2deg(circ_dist(deg2rad(350), deg2rad(15)))
ans =
-25
If you have Matlab's Robotics Systems Toolbox, you could use angdiff(). Note the opposite sign of the outputs compared to circ_dist().
>> rad2deg(angdiff(deg2rad(15),deg2rad(350)))
ans =
-25
>> rad2deg(angdiff(deg2rad(350),deg2rad(15)))
ans =
25
Compare these to Matlab's wrapTo360() which only wraps values between [0,360] so 350-15 would not indicate the closest circular distance.
>> wrapTo360(15-350)
ans =
25
>> wrapTo360(350-15)
ans =
335
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!