cart2pol angle sin 0 to 2pi range instead of -pi to pi range?

조회 수: 27 (최근 30일)
Alexander Hannides Rajapakse
Alexander Hannides Rajapakse 2018년 5월 28일
답변: theodore panagos 2018년 11월 7일
I have a huge set of x and y coordinates that I have converted to polar coordinates using cart2pol, but I need to have my range of angles to be 0 to 2pi instead of -pi to pi. How can I do this without manually adding 2pi to each negative theta?

답변 (2개)

John D'Errico
John D'Errico 2018년 10월 24일
The most efficient solution is to use mod. Thus...
theta = mod(cart2pol(x,y),2*pi);
This works, because what you really want is to add 2*pi to the angle, whenever the angle is less than zero. mod does exactly that. And, yes, mod works for non-integer moduli, and of course mod is vectorized, so it does everything you need without even a need for a loop.

theodore panagos
theodore panagos 2018년 11월 7일
You can use the formula:
atan(x,y)=pi()-pi()/2*(1+sign(x))*(1-sign(y^2))-pi()/4*(2+sign(x))*sign(y)
-sign(x*y)*atan((abs(x)-abs(y))/(abs(x)+abs(y)))
there is x=x2-x1 and y=y2-y1
The formula give the angle from 0 to2pi for all the values x and y
For x=y=0 the result is undefined.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by