a(a(a>3)<10) = OK ; a(a>3 && a<10) = wrong!, the correct way?

1 vue (au cours des 30 derniers jours)
Leotsing
Leotsing le 27 Fév 2017
Modifié(e) : Leotsing le 27 Fév 2017
a=[1 2 3 4 11 33]; I want to get the number in a from 4-9; a(a(a>3)<10) is right; a(a>3 && a<10) is wrong -- is there other correct way to make this ?

Réponse acceptée

Alexandra Harkai
Alexandra Harkai le 27 Fév 2017
If you want to get only values between 3 and 10 (excluding the endpoints), the correct way would be:
a(a>3 & a<10)
(Not exactly sure what you meant by 'OK' but a(a(a>3)<10) definitely gives some unexpected results.)
  2 commentaires
John D'Errico
John D'Errico le 27 Fév 2017
Note that Alexandra used a single & operator there. This is correct when you are anding together boolean vectors. Use && only in things like a test in an if statement.
Leotsing
Leotsing le 27 Fév 2017
Modifié(e) : Leotsing le 27 Fév 2017
thank you and other people answered! that"OK" means it works well to the result,but I think it is not good,so I asked.BTW, I also thought about FIND function,but never come to "&" .thank you again!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Modeling dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by