# if a & b

22 Ansichten (letzte 30 Tage)
Skander Marouani am 13 Mär. 2018
Bearbeitet: Stephen23 am 14 Mär. 2018
i tiped this condition if m <= 0 && i > 1 and matlab is blocking it and this is the error msg Operands to the and && operators must be convertible to logical scalar values. some one can help me ?
##### 5 Kommentare3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden
Stephen23 am 14 Mär. 2018
Bearbeitet: Stephen23 am 14 Mär. 2018
@Skander Marouani: beginners are often confused by the behavior of non-scalar conditions for if, while, etc. You will find plenty of examples on this forum, e.g.:
etc, etc, etc. But luckily, as I wrote in my earlier comment, this confusion is trivial to avoid by simply only using scalar conditions, and using any, all, ||, and && as required, which make the code intent perfectly clear. So you should not swap && for & as you have done, but explicitly add any or all as fits your logical conditions.
Guillaume am 14 Mär. 2018
Yes, as Stephen says you did not solve the problem. You got rid of the error by introducing a subtler problem which only occurs under some circumstance because you didn't actually understand the cause of the problem.
Chances are you will come across the new problem 5 minutes before an important deadline.
If you really want to solve the problem then you need to tell us:
- was it intended that m or i is a vector. If not, then that's the first bug you need to fix
- if it was intended, then you need to explain if the if applies when all the values are greater (or smaller) than the threshold or only some of them.
It's also very possible that the if is not even needed, depending on what you are doing.

Melden Sie sich an, um zu kommentieren.

### Antworten (2)

Greg am 14 Mär. 2018
This means m or i (or both) are not convertible to logical scalar values.
They could be not convertible to logical (is {'Hello World'} true or false?), or they aren't scalar (is [true, false, true, false] true or false?).
Also, your question title is explicitly different from your question. The commands a & b and a && b are drastically different.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Roger Stafford am 14 Mär. 2018
You can't use the "short circuit" forms of logical operations for operands other than scalars. The double symbol '&&' is the short circuit form of '&'.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Common Operations finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by