when I put a one variable, a function says A, but when I put a variable on row it says B
2 views (last 30 days)
Show older comments
I made a function to make radian to degree.
And the result have to -180<(degree)<180. So 'r2d' is works well.
but If I put the syntax like " radian = [-5/2*pi:pi/5:5/2*pi] " to function on script, it dosent work well.
How can fix them?
script is :
clear, clc
%임의의 radian을 degree로
radian = [-5/2*pi:pi/5:5/2*pi] ;
degree = r2d(radian) ;
r_d = [radian; degree]'
function r2d is :
function R = r2d(x)
% radian을 degree로 변환
f = x./pi ;
R = f.*180 ;
% radian이 pi 초과인 경우
if f>1
cost = fix(f) ;
R = 180 .* (f - fix(f)) ;
if R == 0
R = 180
end
% radian이 -pi 미만인 경우
elseif f<-1
cost = fix(f) ;
R = 180 .* (f - fix(f)) ;
if R == 0
R = -180
end
% pi가 -pi 이상 pi 이하일 경우
else
end
Answers (1)
Shadaab Siddiqie
on 14 Apr 2021
Edited: Shadaab Siddiqie
on 14 Apr 2021
From my understanding you want to convert radians to degree such that result is in the range [-180,180]. It is unclear if your r2d(0) should output 0 or -180. But here is a solution which might help you.
function degrees = Untitled2(varargin)
radang = cell2mat(varargin);
if nargin < 1
error('Invalid number of inputs');
end
if ischar(radang)
error('Input must be a angle or a vector of angles')
end
degreesNotInRange = rem(360+radang*180/pi,360);
degrees = rem(degreesNotInRange,360)-180;
You can run it
>> r2d(pi,pi/6,pi/4,pi/2,2*pi/3)
>> r2d(1.48)
>> r2d(0:pi/6:pi)
0 Comments
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!