Logical 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.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

How to Solve Differential Equations in MATLAB (ode45, ode15s, bvp4c)

Differential equation assignments usually boil down to three scenarios: standard initial value problems, stiff systems that crash normal solvers, and boundary value problems whe...

MATLAB Guide 5 Min Read

Physics-Informed Neural Networks (PINNs) in MATLAB: Complete Guide

1. Why Standard AI Fails on Real-World Physics Problems If you've ever tried training a standard deep learning model to predict fluid dynamics, structural stress, or h...