when I put a one variable, a function says A, but when I put a variable on row it says B

조회 수: 2 (최근 30일)
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

답변 (1개)

Shadaab Siddiqie
Shadaab Siddiqie 2021년 4월 14일
편집: Shadaab Siddiqie 2021년 4월 14일
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)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by