problem with if statement

조회 수: 4 (최근 30일)
NS
NS 2011년 11월 4일
Hi Guys,
This is a very basic question. I want to figure out which quadrant a point lies in from the value I get from atan2. I dont know if there is a simpler way but I wrote a code containing a series of if statements and it isnt executing right. Can anyone tell me what is wrong in it. Your help is greatly appreciated.
if 0<ang<1.5708
quad=1;
end
if 1.5708<ang<3.1416
quad=2;
end
if -1.5708<ang<0
quad=3;
end
if -3.1416<ang<-1.5708
quad=4;
end
Thanks, NS
  댓글 수: 1
sco1
sco1 2011년 11월 4일
I'll look at it in a second, but I would suggest using pi() instead of discrete numbers in order to avoid errors where your point lies on one of the axes.

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

채택된 답변

Wayne King
Wayne King 2011년 11월 4일
if (0<ang && ang <1.5708)
quad=1;
elseif (1.5708<ang && ang<3.1416)
quad=2;
elseif (-1.5708<ang && ang<0)
quad=3;
else
quad=4;
end
You can't do 0<ang<pi/2
  댓글 수: 1
NS
NS 2011년 11월 4일
Thanks. Works nicely.

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 4일
What is wrong is that "0<ang<1.5708" is not a valid statement according MATLAB M-language syntax. You will need to use
and(0<ang, ang<1.5708)
Or
0<ang && ang<1.5708
  댓글 수: 1
NS
NS 2011년 11월 4일
Oh I didnt know that. Thanks I will change my code :)

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


sco1
sco1 2011년 11월 4일
I don't think MATLAB will handle the logic statements when put that way, try using an and statement instead: I would also suggest using elseif just because it makes things look neater.
For example:
if ang > a && ang < b
quad = x
elseif ang > b && ang < c
quad = y
etc.
edit: I'm a slow typer!
  댓글 수: 1
NS
NS 2011년 11월 4일
Thanks Sam. :)

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by