How to make negative degree numbers positive while maintaining degree.

26 次查看(过去 30 天)
I'm trying to use
if avg_deg < 0, avg_deg = avg_deg+360; end
but it isn't working. I don't want to use abs because I want to rotate these negative degrees to their positive counterpart, but the line of code isn't doing anything at all.

采纳的回答

Walter Roberson
Walter Roberson 2021-10-21
You can use logical indexing....
avg_deg = randi([-719 719], 1, 10)
avg_deg = 1×10
149 -15 -420 453 593 -132 594 712 344 423
mask = avg_deg < 0;
new_deg_version_1 = avg_deg;
new_deg_version_1(mask) = new_deg_version_1(mask) + 360
new_deg_version_1 = 1×10
149 345 -60 453 593 228 594 712 344 423
But I would suggest that mod() is more reliable, since it can handle angles < -360 and angles >= 360
new_deg_version_2 = mod(avg_deg, 360)
new_deg_version_2 = 1×10
149 345 300 93 233 228 234 352 344 63
  2 个评论
Walter Roberson
Walter Roberson 2021-10-22
编辑:Walter Roberson 2021-10-22
avg_deg = [randi([-719 719], 1, 5), -720, -360, 0, 360, 720]
avg_deg = 1×10
-617 -202 -77 -35 -677 -720 -360 0 360 720
new_deg_version_2 = mod(avg_deg, 360)
new_deg_version_2 = 1×10
103 158 283 325 43 0 0 0 0 0
new_deg_version_3 = wrapTo360(avg_deg)
new_deg_version_3 = 1×10
103 158 283 325 43 0 0 0 360 360
They differ on the treatment of positive multiples of 360.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by