question about the number format in conditional statements
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Rik
2020-6-11
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 个评论
Walter Roberson
2020-6-11
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!