Open In App

Operator Overloading in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

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 
    • a * a
  • 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 :
    • [9,8,7,6,5,4,3,2,1,0]
  • 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 ( ==, != >,<)


Last Updated : 05 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads