Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining additional infix operators in MATLAB

Tags:

matlab

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.

like image 419
Sam Roberts Avatar asked Nov 16 '11 13:11

Sam Roberts


2 Answers

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.

like image 193
Phonon Avatar answered Nov 06 '22 05:11

Phonon


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.

like image 25
bla Avatar answered Nov 06 '22 05:11

bla