why the give code doesn't meet my requirements?

6 ビュー (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2024 年 9 月 15 日
編集済み: dpb 2024 年 9 月 22 日
I want to Wrap angles to the range [-180, 180] degrees. For this I have the following code:
function wrapped_angles = wrapTo180(angles)
% Wrap angles to the range [-180, 180] degrees
wrapped_angles = mod(angles + 180, 360) - 180;
end
Likewise, I want Wrap angles to the range [-90, 90] degrees. For this I have the following code:
function wrapped_angles = wrapTo90(angles)
% Wrap angles to the range [-90, 90] degrees
wrapped_angles = mod(angles + 90, 180) - 90;
end
The 2nd function works correctly but the 1st one doesn't work correctly. Why it is so?
  3 件のコメント
Sadiq Akbar
Sadiq Akbar 2024 年 9 月 15 日
Thank you very much for your kind response. see below:
clear;clc
angles = [-182 -185 189 184];
% angles = [-150 -32 32 189];
wrapped_angles = wrapTo180(angles);
% wrapped_angles = wrapTo90(angles);
disp('Final Wrapped Angles:');
disp(wrapped_angles);
This is a test code. When I give the top angles to warp180(angles), it should give: -2 -5 9 4 but it gives: 178 175 -171 -176
Torsten
Torsten 2024 年 9 月 15 日
編集済み: Torsten 2024 年 9 月 15 日
The interval you transform the angle to should have length 360 degrees, shouldn't it ? So I don't understand how you could transform angles uniquely to the interval [-90 90].
For the interval [-180 180], it's ok.
angles = [-182 -185 189 184];
wrapped_angle180 = mod(angles,360) - 180
wrapped_angle180 = 1×4
-2 -5 9 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
wrapped_angle90 = mod(angles,180) - 90
wrapped_angle90 = 1×4
88 85 -81 -86
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2024 年 9 月 15 日
angles = [-182 -185 189 184];
rem(angles,180)
ans = 1×4
-2 -5 9 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
From the doc for rem one learns...
"The concept of remainder after division is not uniquely defined, and the two functions mod and rem each compute a different variation. The mod function produces a result that is either zero or has the same sign as the divisor. The rem function produces a result that is either zero or has the same sign as the dividend."
To use MATLAB mod as you wish, you would have to write your own version of rem --
sign(angles).*mod(abs(angles),180)
ans = 1×4
-2 -5 9 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  8 件のコメント
Torsten
Torsten 2024 年 9 月 22 日
I think the correct answer is that there is no "correct" answer.
One has to decide whether to use "rem" or "mod" or some other normalization to the respective interval.
dpb
dpb 2024 年 9 月 22 日
編集済み: dpb 2024 年 9 月 22 日
That's the point of the Q? -- trying to determine what was/is OP's basis for determining what result satisifed -- and maybe, thereby, clarifying his/her thinking.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by