34.4.2 Operator Overloading

The following table shows, for each built-in numerical operation, the corresponding function name to use when providing an overloaded method for a user class.

a + bplus (a, b)Binary addition
a - bminus (a, b)Binary subtraction
+auplus (a)Unary addition
-auminus (a)Unary subtraction
a .* btimes (a, b)Element-wise multiplication
a * bmtimes (a, b)Matrix multiplication
a ./ brdivide (a, b)Element-wise right division
a / bmrdivide (a, b)Matrix right division
a .\ bldivide (a, b)Element-wise left division
a \ bmldivide (a, b)Matrix left division
a .^ bpower (a, b)Element-wise power
a ^ bmpower (a, b)Matrix power
a < blt (a, b)Less than
a <= ble (a, b)Less than or equal to
a > bgt (a, b)Greater than
a >= bge (a, b)Greater than or equal to
a == beq (a, b)Equal to
a != bne (a, b)Not equal to
a & band (a, b)Logical and
a | bor (a, b)Logical or
!anot (a)Logical not
a'ctranspose (a)Complex conjugate transpose
a.'transpose (a)Transpose
a:bcolon (a, b)Two element range
a:b:ccolon (a, b, c)Three element range
[a, b]horzcat (a, b)Horizontal concatenation
[a; b]vertcat (a, b)Vertical concatenation
a(s_1,…,s_n)subsref (a, s)Subscripted reference
a(s_1,…,s_n) = bsubsasgn (a, s, b)Subscripted assignment
b(a)subsindex (a)Convert object to index
dispdisp (a)Object display

Table 34.1: Available overloaded operators and their corresponding class method

An example mtimes method for the polynomial class might look like

function p = mtimes (a, b)
  p = polynomial (conv (double (a), double (b)));