Conditional statements not catching

조회 수: 3 (최근 30일)
Andrew Waller
Andrew Waller 2016년 9월 16일
편집: Andrew Waller 2016년 9월 30일
I'm trying to check conditions of of pairs of x and y values. Instead of nesting an if statement within another to check x and y, i initially tried to use && feature but kept getting error for "operators must be convertible to logical scalar values". After digging through here, I tried the suggestions to change && to & but logically the code wasn't checking the conditions for each individual x and y scalars in my list.
so now I'm trying to nest my if functions, the code executes but still isn't catching the conditions and triggering what I want it too.
function[theta_r,theta_d] = firstassignment_test(x,y)
theta_r = atan(y./x);
theta_d = theta_r*(180/pi);
if (x>0)
if (y>0)
theta_d = theta_r.*(180/pi);
end
elseif (x<0)
if (y>0)
theta_d = (theta_r+pi).*(180/pi);
end
elseif (x<0)
if (y<0)
theta_d = (theta_r-pi).*(180/pi);
end
elseif (x<0)
if (y==0)
theta_d = pi.*(180./pi);
end
elseif (x==0)
if (y>0)
theta_d = (pi./2)*(180./pi);
end
elseif (x==0)
if (y<0)
theta_d = (-pi./2)*(180./pi);
end
end
  댓글 수: 1
Andrew Waller
Andrew Waller 2016년 9월 16일
Thanks so far on your answers. I'm still new to matlab and its been a while since coding even on c++ so I'm still learning tons with the logic and syntax along with the useful features of matlab.
unfortuantly this particular assignment requires me to use if, elseif statements to solve. The exact assignment is to use the function "atan(y/x)" in combination with if statements to convert from (x,y) coordinates to polar coordinates. So my if statements should appropriately add and subtract pi to accurately output if in quadrant 2 or 3.

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

채택된 답변

James Tursa
James Tursa 2016년 9월 16일
편집: James Tursa 2016년 9월 16일
Looks like your code is intended to have arrays as inputs. Because of this, none of your if-else-etc checking is going to work as you expected. E.g., take the first check:
if (x>0)
If x is a vector, some of the elements could satisfy this and some may not. So what do you expect to happen here? Here is what happens according to the doc for "if", and this is not the behavior that you obviously want:
"An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false."
You could convert each of these blocks to logical indexing instead. E.g., this
if (x>0)
if (y>0)
theta_d = theta_r.*(180/pi);
end
could be converted to this
m = (x>0) & (y>0); % Indexes that you want
theta_d(m) = theta_r(m).*(180/pi); % Operate only on those indexes
Similarly for the other blocks.
  댓글 수: 2
James Tursa
James Tursa 2016년 9월 16일
편집: James Tursa 2016년 9월 16일
If you are required to use if-else statements, then you need to wrap everything in a loop to work on each element individually. E.g.,
for k=1:numel(x)
if (x(k)>0)
if (y(k)>0)
theta_d(k) = theta_r(k).*(180/pi);
end
:
etc
:
end
That's assuming x and y are the same size. If you allow for one of them to be a scalar, then extra code would need to be added to account for that.
Andrew Waller
Andrew Waller 2016년 9월 17일
편집: Andrew Waller 2016년 9월 30일
thanks! worked out perfect when including the for loop. thankfully my x and y arrays were the same size.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by