Is there a way to define additional infix operators in MATLAB?
Specifically, I'd like to define two infix operators ->
and <->
(these symbols would be ideal, but it could be a single character if necessary), which call functions implies
and iff
in the same way that &
calls and
and +
calls plus
.
function z = implies(x, y)
z = ~x|y;
function z = iff(x, y)
z = x&y | ~x&~y;
I'm happy to overload logical
if necessary.
There is no way to define new Operators in MATLAB as several threads like this one suggest. However, if you'd like to overload an existing operator for you own class, here's MATLAB's documentation page, though I'm sure you've already seen it.
What about using operator
? it is used to define new user-defined operator symbols or to delete them.
operator(symb, f, T, prio)
defines a new operator symbol symb
of type T
with priority prio
. The function f
evaluates expressions using the new operator.
Given the operator symbol "++", say, with evaluating function f
, the following expressions are built by the parser, depending on the type of the operator:
Prefix: The input ++x results in f(x).
Postfix: The input x++ results in f(x).
Binary: The input x ++ y ++ z results in f(f(x, y), z).
Nary: The input x ++ y ++ z results in f(x, y, z)).
see more at matlab's documentation in the link above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With