Error in repetition of for loop

조회 수: 2 (최근 30일)
Fu Xiangli
Fu Xiangli 2016년 9월 7일
편집: mbonus 2016년 9월 8일
Hi guys. I have a code below:
if p && u <= length(r) && r(p) > r(n) && r(u) >= r(p);
is_upslope = true;
However, when my u is greater than the length(r), it still gives me is_upslope = true. May I know what is the problem? Thanks
  댓글 수: 1
per isakson
per isakson 2016년 9월 7일
I cannot reproduce your result. What values do your variables have?

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

답변 (1개)

mbonus
mbonus 2016년 9월 7일
편집: mbonus 2016년 9월 7일
I believe that your second test of <=length(r) is being tested against a logical 1. This is due to the fact that p && u is being evaluated first which returns a logical 1 assuming they are both nonzero. so the test actually ends up seeing
1 <= length(r) ...
instead you need to format it like this: (also parenthesis while not required make it easier to read)
if (p <= length(r)) && (u <= length(r)) && (r(p) > r(n)) && (r(u) >= r(p))
  댓글 수: 2
Thorsten
Thorsten 2016년 9월 8일
mbonus
mbonus 2016년 9월 8일
편집: mbonus 2016년 9월 8일
Oh wow I didn't realize MATLAB did it like that, probably because I always use parenthesis, thank you.

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

카테고리

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