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?

回答(1 个)

Rik
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
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 CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by