How To Use | and || Operator in MATLAB?
Last Updated :
24 Apr, 2025
In MATLAB, | and || are both logical operators that are used to perform logical OR operations on Boolean variables, however, there is a subtle difference between the two:
The element-wise logical OR operator "|" takes two arrays of the same size and returns an array of the same size where each element is the result of the logical OR operation between the corresponding elements of the input arrays. For example, if A and B are two arrays of the same size, then A|B will return a new array C of the same size, where each element of C is the result of the logical OR operation between the corresponding elements of A and B.
The | operator is a bitwise OR operator, which compares the binary representation of two values and returns a new value where each bit is set to 1 if either or both of the corresponding bits in the original values are 1. For example:
Matlab
A = [1 0 0; 1 1 1];
B = [1 1 0; 0 1 0];
C = A | B;
In this example, C will be equal to [1 1 0; 1 1 1], because each element of C is the bitwise OR of the corresponding elements in A and B.
The short-circuit logical OR operator "||" takes two scalar inputs and returns a scalar output. It operates by first evaluating the first input. If the first input is true, then the operator returns true without evaluating the second input. If the first input is false, then the operator evaluates the second input and returns its value.
The || operator is a logical OR operator, which compares two values and returns a Boolean value of true if either or both of the values are true. For example:
Matlab
A = [1 0 0; 1 1 1];
B = [1 1 0; 0 1 0];
C = A || B;
In this example, C will be equal to [1 1 0; 1 1 1], because each element of C is the logical OR of the corresponding elements in A and B.
As a general rule, it is recommended to use the || operator when working with logical expressions, and the | operator when working with bitwise operations.
Example 1:
Matlab
A = 5; % binary representation of 5 is 101
B = 3; % binary representation of 3 is 011
result = A | B;
disp(result);
Output:
In this example, the binary representation of A is 101 and the binary representation of B is 011. When using the bitwise OR operator |, it compares each bit of the binary representation of A and B.
1 OR 0 = 1, 0 OR 1 = 1, 1 OR 1 = 1.
So, the result will be 101 (5) | 011 (3) = 111 (7).
Example 2:
Matlab
A = true;
B = false;
result = A || B;
disp(result);
Output:
In this example, we are using the logical OR operator || to check if either A or B is true. This program sets the value of variable A to true, which is equivalent to the boolean value 1, and the value of variable B to false, which is equivalent to the boolean value 0. The program then uses the logical OR operator || to perform a logical OR operation on the two boolean values. Since the OR operator returns true if at least one of the operands is true, the result of the operation, in this case, is true, which is equivalent to the numerical value 1. Finally, the program uses the disp function to display the value of the result variable, which is 1.
It's worth mentioning that the logical OR operator || will only evaluate the second operand if the first operand is false, this is known as short-circuit evaluation.
Example 3:
Matlab
year = 2020; % replace with your desired year
if mod(year, 4) == 0 && (mod(year, 100) ~= 0 || mod(year, 400) == 0)
fprintf('%d is a leap year. \n', year);
else
fprintf('%d is not a leap year. \n', year);
end
Output:
Explanation:
This code uses the modulus operator mod to check whether the year is evenly divisible by 4. If it is, the code then checks whether it is evenly divisible by 100. If it is not, then it is a leap year. If it is evenly divisible by 100, the code then checks whether it is evenly divisible by 400. If it is, it is a leap year; if it is not, it is not a leap year. The fprintf function is used to print a message indicating whether the year is a leap year or not.
Similar Reads
How to Use & and && Operator in MATLAB?
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of
3 min read
Operator Overloading in MATLAB
MATLAB allows you to specify more than one definition for an operator in the same scope which is called Operator Overloading. We can redefine or overload most of the built-in operators available in MATLAB. It is basically a type of polymorphism in which an operator is overloaded to give user-defined
3 min read
How to remove decimal in MATLAB?
In this article, we are going to discuss the "Removal of decimal point" in MATLAB which can be done using sprintf(), fix(), floor(), round() and num2str() functions which are illustrated below. Using sprintf() The sprintf() function is used to write formatted data to a string. Syntax: sprintf(format
3 min read
How to Append Data to a File in MATLAB?
Appending data to a text file means adding data to a file that already exists in the storage. print() function is used to write/append data to a file in MATLAB. It writes formatted text to a file based on the format string provided to it. The format of the output/input is determined by the formattin
4 min read
How to Remove Nan Values from a Matrix in MATLAB?
Removal of Nan Values from a Matrix.There are multiple methods by which we can remove Nan values from a specified matrix:. Method 1: By using rmmissing( )Â This function is used to remove missing entries or Nan values from a specified matrix. Syntaxrmmissing(A) Parameters: This function accepts a pa
2 min read
Negative of an image in MATLAB
The negative of an image is achieved by replacing the intensity 'i' in the original image by 'i-1', i.e. the darkest pixels will become the brightest and the brightest pixels will become the darkest. Image negative is produced by subtracting each pixel from the maximum intensity value. For example i
2 min read
Column Vectors in MATLAB
Column vectors are vectors that have a single column but, multiple rows. MATLAB provides various functions that use column vectors as their arguments. In this article, we will see different methods of creating and using column vectors in MATLAB. Creating Column Vectors:Method 1:The simplest way of c
2 min read
How to Find Indices and Values of Nonzero Elements in MATLAB?
MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector. Syntax: vec = [] %so
3 min read
Write Data to Text Files in MATLAB
Writing data to a text file means creating a file with data that will be saved on a computer's secondary memory such as a hard disk, CD-ROM, network drive, etc. fprintf() function is used to write data to a text file in MATLAB. It writes formatted text to a file exactly as specified. The different e
3 min read
Image Processing in MATLAB | Fundamental Operations
1. Reading Images Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument For Example: >> I=imread('nature.jpg'); This will read JPEG image 'nature' into the image array. Note: The semicolon(;) at the end of command lin
3 min read