ogical Operators & | ~ : Logical Operators MATLAB Proramming

MATLAB Illustration

Introduction

Logical operators are essential in MATLAB for performing conditional checks, array comparisons, and controlling the flow of programs. MATLAB supports the following main logical operators:

  • AND (&) – Returns true if both conditions are true.

  • OR (|) – Returns true if at least one condition is true.

  • NOT (~) – Returns true if the condition is false (logical negation).

Logical operators can be applied to scalars, vectors, matrices, or arrays, making them extremely powerful for mathematical computations and programming.


1. AND Operator (&)

The & operator evaluates two conditions and returns true (1) only if both conditions are true.

Example:

 
a = 5; b = 10; result = (a > 0) & (b > 5); % Both conditions are true disp(result) % Output: 1

Array Example:

 
x = [1 2 3 4 5]; y = [5 4 3 2 1]; result = (x > 2) & (y < 4); disp(result) % Output: [0 0 1 0 0]

2. OR Operator (|)

The | operator returns true (1) if at least one condition is true.

Example:

 
a = 3; b = 7; result = (a > 5) | (b > 5); % Only one condition is true disp(result) % Output: 1

Array Example:

 
x = [1 2 3 4 5]; y = [5 4 3 2 1]; result = (x < 3) | (y < 3); disp(result) % Output: [1 1 0 1 1]

3. NOT Operator (~)

The ~ operator negates a logical condition. If a condition is true, ~ makes it false, and vice versa.

Example:

 
a = 10; result = ~(a > 5); % Negates the condition disp(result) % Output: 0

Array Example:

 
x = [1 2 3 4 5]; result = ~(x > 3); disp(result) % Output: [1 1 1 0 0]

4. Combining Logical Operators

Logical operators can be combined to form complex conditional statements:

 
x = 7; y = 4; result = ((x > 5) & (y < 5)) | ~(x < 10); disp(result) % Output: 1

This makes MATLAB ideal for conditional checks, filtering arrays, and controlling program flow.


5. Practical Applications

  • Filtering arrays: Extract elements meeting multiple conditions.

  • Conditional loops: Use logical operators in if, while, or for statements.

  • Data analysis: Compare datasets element-wise using logical operations.

  • Control systems and simulations: Logical operators are widely used in system logic and MATLAB Simulink conditions.


Conclusion

Logical operators &, |, and ~ are fundamental in MATLAB programming for making decisions, filtering data, and performing complex array operations. By mastering these operators, you can write more efficient and readable MATLAB code and handle both scalar and array-based conditions effortlessly.

What Our Students Say

★★★★★

“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”

Aditi Sharma, Mumbai
★★★★☆

“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”

John M., Australia

Latest Blogs

Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.

Most Trending Topics in MATLAB Simulation in 2026

MATLAB and Simulink continue to be powerful tools for modeling, simulation, and system design across engineering domains. From electric vehicles to smart grids and AI-driven automation, MATLAB simulation is playing a critical role in modern research and industry ap

Multimodal AI Breakthroughs: Implementing Text + Image + Audio Models with MATLAB

Multimodal AI is one of the most exciting frontiers in artificial intelligence research today. In early 2026, multimodal models-systems that process and reason across text, images, audio, and eve