Operator Overloading in MATLAB
Last Updated :
28 Apr, 2025
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 meaning to it. By implementing operators that are appropriate to your class, we can integrate objects of our class into the MATLAB language. Almost any operator can be overloaded in MATLAB. For example, objects that contain numeric data can define arithmetic operations like +, *, -, / etc so that we can use these objects in arithmetic expressions. By implementing relational operators, you can use objects in conditional statements, like switch, and if statements.
Overloaded operators retain the original MATLAB precedence for the operator.
We can understand it clearly with the help of examples. The Multiplication class implements multiplication for objects of this class by defining a Multiplier method. Multiplier defines the multiplication of objects as the multiplication of the NumericData property values. The Multiplier method constructs and returns a multiplication object whose NumericData property value is the result of the multiplication.
Example 1:
Matlab
% MATLAB code
classdef Multiplier
properties
NumericData
end
methods
function obj = Multiplier(val)
obj.NumericData = val;
end
function r = multiply(obj1,obj2)
a = double(obj1);
b = double(obj2);
r = Multiplier(a * b);
end
function d = double(obj)
d = obj.NumericData;
end
end
end
Output:
Explanation:
- Here in this code we first created a class name Multiplier then we define the function and take inputs from the user and work on provided inputs to get the desired output
- Code explanation to take input and generate output :
- Using a double converter enables you to multiply numeric values to Multiply objects and to perform Multiplication on objects of the class.
- a = Multiplier(1:10)
- a = [1,2,3,4,5,6,7,8,9,10]
- Now We multiply the values of the same objects
- Output is : [ 1,4,9,16,25,36,49,64,81,100]
- Now Multiply an object with any value that can be cast to double:
- b = unit8(10)
- [10,10,10,10,10,10,10,10,10,10]
- Now we multiply two different objects :
- [10,40,90,160,250,360,490,640,810,1000]
Now for better or proper understanding, we take another example. Here, the Subtracter class implements subtraction for objects of this class by defining a Subtracter method.
Example 2:
Matlab
% MATLAB code
classdef Subtracter
properties
NumericData
end
methods
function obj = Subtracter(val)
obj.NumericData = val;
end
function r = minus(obj1,obj2)
a = double(obj1);
b = double(obj2);
r = Subtracter(a - b);
end
function d = double(obj)
d = obj.NumericData;
end
end
end
Output:
Code explanation to take input and generate output :
- Using a double converter enables you to minus numeric values from Subtracter objects and to perform Subtraction on objects of the class.
- a = Subtracter(1:10)
- a = [1,2,3,4,5,6,7,8,9,10]
- Now We minus the values of the same objects
- a - a
- Output is : [ 0,0,0,0,0,0,0,0,0,0]
- Now Subtract an object with any value that can be cast to double:
- b = unit8(10)
- [10,10,10,10,10,10,10,10,10,10]
- Now we minus two different objects :
- So this is the concept of operator Overloading in MATLAB. it is a similar language to others C++, java, etc so it also has the same type of operators like :
- Unary operator (-a,+a)
- Arithmetic operators (+,-,*,/)
- Logical operators (&&,||)
- Relational Operators ( ==, != >,<)
Similar Reads
Operator Overloading in Julia Operator overloading in Julia refers to the ability to define custom behavior for built-in operators such as +, -, *, /, etc. when applied to objects of custom types. This allows users to create intuitive and concise code that works with custom data types as if they were built-in. In Julia, operator
5 min read
Input/Output Operators Overloading in C++ Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We c
2 min read
Operator Overloading in Programming Operator Overloading is a feature in some programming languages used to redefine or "overload" the standard behavior of operators (such as +, -, *, etc.) to work with user-defined data types. This is useful when working with objects of custom classes. In this article, we will learn about the basics
4 min read
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
How To Use | and || Operator in MATLAB? 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 e
4 min read
Object Oriented Programming (OOPs) in MATLAB Object-Oriented Programming (OOPs) in MATLAB is similar to many conventional programming languages like Java, Python, etc except the syntax. The important feature of OOPs is, it enables you to combine data and it's associated actions (methods/functions) into objects. Compared to other languages, suc
6 min read