question about the number format in conditional statements

3 vues (au cours des 30 derniers jours)
G L
G L le 11 Juin 2020
Commenté : Walter Roberson le 11 Juin 2020
In conditional statements the "logical" number format is normally used.
What is this number format and why it is obvious to use the logical number format in this context?

Réponses (1)

Rik
Rik le 11 Juin 2020
The logical data type encode either true or false. That makes it an obvious choice for conditional statements, as you are looking for a yes/no answer. If I ask you if you are right-handed, you should answer with 'yes' or 'no', not '5'.
Matlab hides how it internally represents a logical, but a single bit would be enough (whos shows an entire byte is used).
  1 commentaire
Walter Roberson
Walter Roberson le 11 Juin 2020
MATLAB does not formally document how logical is represented, but the external interface says that it is byte. It is possible to use external code that uses uint8 values other than 0 and 1 for logical data type; MATLAB will consider 0 to be false and non-zero to be true (and nan to be an error.)
There are some situations in Simulink where logical values can get packed down to single bits.
There are hardware reasons why 0 vs non-zero can be more efficient than other choices: it is common for there to be a Condition Code Register that gets set "for free" and for there to be specialized instructions that can test that Condition Code Register without having to refer to memory.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical 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