question about the number format in conditional statements

조회 수: 3 (최근 30일)
G L
G L 2020년 6월 11일
댓글: Walter Roberson 2020년 6월 11일
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