question about the number format in conditional statements
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (1)
Rik
am 11 Jun. 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 Kommentar
Walter Roberson
am 11 Jun. 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.
Siehe auch
Kategorien
Mehr zu Logical 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!