why the give code doesn't meet my requirements?

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 comentarios

Stephen23
Stephen23 el 15 de Sept. de 2024
"The 2nd function works correctly but the 1st one doesn't work correctly"
Please given an example input value and the corresponding incorrect output value.
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 el 15 de Sept. de 2024
Editada: Torsten el 15 de Sept. de 2024
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>

Iniciar sesión para comentar.

Respuestas (1)

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 comentarios

dpb
dpb el 15 de Sept. de 2024
The same information as above is also in the doc for mod
Thanks to all of your for your kind responses. As I was busy in some other activities, so couldn't give attention here. Let me explain my issue in simple words: I have two vectors u and b. I want to restric their entries either within 0-360 or within -180 180. i.e.,
clear;clc
u = [-35 -45 -55 35 45 55]; % 6 variables within [-180,180]
b = [30 45 55 135 245 355]; % 6 variables within [0,360]
P = numel(b) / 2; % Number of sources
M = numel(b); % Total number of elements
% Precompute angles for efficiency
var1_u = u(1:P);
var2_u = u(P+1:end);
var1_b = b(1:P);
var2_b = b(P+1:end);
% Wrap angles between 0 and 180 degrees
var1_u = rem(var1_u, -180);
var2_u = mod(var2_u, 180);
var1_b = mod(var1_b, 0);
var2_b = mod(var2_b, 360);
I tried differently but in vain.
Torsten
Torsten el 21 de Sept. de 2024
Editada: Torsten el 21 de Sept. de 2024
mod(x,360)
restricts x to be within 0 and 360,
mod(x,360) - 180
restricts x to be within -180 and 180.
Of course, there are different ways to do with different results ( I say this just in case that you want to respond that you don't like what you get ).
u = [-35 -45 -55 35 45 55]; % 6 variables within [-180,180]
b = [30 45 55 135 245 355]; % 6 variables within [0,360]
mod(u,360) - 180
ans = 1×6
145 135 125 -145 -135 -125
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mod(b,360)
ans = 1×6
30 45 55 135 245 355
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dpb
dpb el 21 de Sept. de 2024
Editada: dpb el 21 de Sept. de 2024
u = [-35 -45 -55 35 45 55]; % 6 variables within [-180,180]
b = [30 45 55 135 245 355]; % 6 variables within [0,360]
rem(u,180)
ans = 1×6
-35 -45 -55 35 45 55
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
rem(b,360)
ans = 1×6
30 45 55 135 245 355
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What would you expect the answers to be if not the above (and, more importantly, why/how do you determine that?). You can't write an algorithm until you can define the problem uniquely.
Sadiq Akbar
Sadiq Akbar el 22 de Sept. de 2024
Thanks a lot to both of you for your kind responses.
dpb
dpb el 22 de Sept. de 2024
So what was/is the correct answer?
Torsten
Torsten el 22 de Sept. de 2024
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 el 22 de Sept. de 2024
Editada: dpb el 22 de Sept. de 2024
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.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 15 de Sept. de 2024

Editada:

dpb
el 22 de Sept. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by