Error in repetition of for loop

1 vue (au cours des 30 derniers jours)
Fu Xiangli
Fu Xiangli le 7 Sep 2016
Modifié(e) : mbonus le 8 Sep 2016
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 commentaire
per isakson
per isakson le 7 Sep 2016
I cannot reproduce your result. What values do your variables have?

Connectez-vous pour commenter.

Réponses (1)

mbonus
mbonus le 7 Sep 2016
Modifié(e) : mbonus le 7 Sep 2016
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 commentaires
Thorsten
Thorsten le 8 Sep 2016
mbonus
mbonus le 8 Sep 2016
Modifié(e) : mbonus le 8 Sep 2016
Oh wow I didn't realize MATLAB did it like that, probably because I always use parenthesis, thank you.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by