0
votes

The assignment is: Reset the values stored in the matrix accordingly, so that they lie within this range [-pi,pi]. Take care not to use any non-standard Matlab function(s) to do this.

(of course are all angles theta + 2n*pi the same, for any integer n. Therefore for example: an angle of 1.5*pi should be reset to -????0.5*pi.)

The nonstandard matlabfunction wraptopi does this (I think), but I am not allowed to use this function. I've got the feeling that I could use modulo to do this, but I don't know how.

Could anyone help me please?

Thanks in advance

1
Modulo will work. When you want to wrap onto a range that doesn't start at zero [0... N), you can add an offset, do modulo, then subtract the offset out again.Ben Voigt
Thanks a lot! I'm almost there. However, when I compare my result with what wraptopi does, my values are on avarage a bit to big... This is what I did: %Start off with matrix D offset=max(max(D(:,:))); % determine the offset: I take the max value of the matrix D_with_offset=D+offset; % add the offset D_mod=mod(D_with_offset,(2*pi)); % take the modulo of all values, with 2pi D_back_within_range=D_mod-pi; % remove the offset again Could someone tell me where this goes wrong?rasa
Your offset should just be pi, not the max of the matrix. Same value you subtract out at the end.Ben Voigt
Thanks a lot! It works! :)rasa

1 Answers

4
votes

To expand on @Ben Voight, you could use modulo-style operations this way:

To wrap to [0, 2*pi], you'd do this:

angle_rad = angle_rad - 2*pi*floor(angle_rad/(2*pi));

To wrap to [-pi, +pi], you'd add another term

angle_rad = angle_rad - 2*pi*floor( (angle_rad+pi)/(2*pi) );