Skip to main content
Logo image

Subsection Logical Values: true/false (Answers)

MATLAB has two logical values: true and false. When displayed, they often appear as logical 1 and logical 0. Although they behave similarly to the numeric values 1 and 0, they are different data types. Just as numbers can be stored in variables, logical values can be stored in variables as well.
a = 1;
b = true;
c = 0;
d = false;

fprintf("a = %g,  b = %g,  c = %g,  d = %g\n", a, b, c, d)
Running the above code, fprintf produces the following output:
a = 1,  b = 1,  c = 0,  d = 0
Even though a and b (and c and d) look the same, MATLAB treats them differently: a is a number (a double), while b is a logical value. You can get more information using the whos command. Here is the output you should see:
Name        Size            Bytes  Class      Attributes
a           1x1                 8  double
b           1x1                 1  logical
c           1x1                 8  double
d           1x1                 1  logical
This shows us two differences:
Class (data type)
a is a double, while b is a logical.
Bytes (memory usage)
a uses 8 bytes, while b uses 1 byte.
At the end of the day, the double data type is meant to store numeric quantities, while the logical data type stores yes/no answers to questions.

Exercises 🤔💭 Conceptual Questions

1.

(a) Logical Data Types.
    In MATLAB, true and 1 are the same.
  • True.

  • Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.
  • False.

  • Incorrect. While true and 1 are represented by the same numeric value, they have different data types: true is of type logical, while 1 is of type double.
(b) Understanding Logical Values.
Why is true (logical) different from 1 (double) in MATLAB, even though they display similarly?
  • true represents a logical value, while 1 is numeric
  • Correct! Logical values are designed to represent yes/no answers to questions, while numeric values represent measurements or counts.
  • They are the same and can be used interchangeably
  • Incorrect. While they behave similarly in many contexts, they are different data types with different purposes.
  • Logical values are faster than numeric values
  • Incorrect. While logical values do use less memory, the key difference is their purpose: answering yes/no questions versus storing numeric data.