Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radians to degrees Matlab

I want to convert angles from radians to degress. I know that Matlab 2014 has a build-in function to do that, but that function is not good enough in my specific case. Basically, the angle pi/2 is 90deg and -pi/2 is also 90deg, however I need 270deg as answer for -pi/2. Think of it as the radians/degrees circle, and I need to count the angle counterclockwise each time.

The function which I have made is the following:

function [Angle_deg] = Func_Rad2Deg(Angle_rad)
Angle_deg = Angle_rad * (180/pi);
if Angle_deg < -1 
    Angle_deg = Angle_deg + 360;

elseif Angle_deg >= -1e-6 && Angle_deg <= 1e-6
    Angle_deg = 0;
end
end

However when I test it using the following (simple) example something goes wrong.

clear all; close all; clc;
%% Tester
vec = [pi -pi pi/2 -pi/2];
vec_deg1 = Func_Rad2Deg(vec);
for i=1:size(vec,2)
    vec_deg2(i) = Func_Rad2Deg(vec(i));
end

Output:

vec_deg1 = 
180  -180    90   -90
vec_deg2 =
180   180    90   270

As it can be seen from the example above, when I try to convert the vector I get incorrect answer for -pi/2. However, when I use a loop the answers are the desired ones. The example is a simple case, in my code I have matrices up to 1000x1000 to convert, and therefore I want to limit the number of for-loops. I was therefore wondering if there is an easier way to do what I want. Maybe I have missed a Matlab function?

Thanks,

like image 518
user5489 Avatar asked Dec 19 '22 07:12

user5489


1 Answers

How about this:

vec_deg2=mod(radtodeg(vec), 360)
like image 123
Cape Code Avatar answered Jan 03 '23 10:01

Cape Code