필터 지우기
필터 지우기

Loop and Conditional Statements

조회 수: 3 (최근 30일)
Paulo Eduardo Beiral
Paulo Eduardo Beiral 2021년 6월 24일
댓글: Paulo Eduardo Beiral 2021년 6월 24일
I've got two vectors 1x366 u and v, and I have to make a new 1x366 vector of the arctg of u and v.
But I've got a few conditions depending if the respective value of u and v are negative or positive.
Here's some of the code which I started writing, but I am stuck.
Thank you for any help.
for n = 1:length(u)
if u(n) <0 && v(n) < 0
atan(v./u);
elseif u(n) > 0 && v(n) < 0
atan(u./v) + 4.712;
elseif u(n) < 0 && v(n) > 0
atan(u./v) + 1.571;
elseif u(n) > 0 && v(n) > 0
atan(v./u) + 3.142;
end
end
  댓글 수: 2
Scott MacKenzie
Scott MacKenzie 2021년 6월 24일
Have you looked at atan2? This is the four-quadrant version of atan -- might be what you need.
Paulo Eduardo Beiral
Paulo Eduardo Beiral 2021년 6월 24일
Unfortunately atan2 wont help me in this case, I need exactly the atan with the conditions I entered in my code.
Thank you, mister.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2021년 6월 24일
As written your code calls atan at most once per element of u but then throws that result away. You also have magic numbers that look like approximations to multiples of pi.
Instead I recommend using logical indexing.
base1 = atan(v./u);
base2 = atan(u./v);
result = NaN(size(u)); % determine what you want result to be if none of the cases hold
case1 = (u < 0) & (v < 0);
result(case1) = base1(case1);
case2 = (u > 0) & (v < 0);
result(case2) = base2(case2) + (3*pi/2);
% and similar for case3 and case4
  댓글 수: 1
Paulo Eduardo Beiral
Paulo Eduardo Beiral 2021년 6월 24일
Can't thank you enough!
My best regards!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by