Search code examples
matlabsyntaxbinary-operators

Why do multiple binary operators in a row work in MATLAB?


Why is this a valid MATLAB query?

3++4

which evaluates to 7. Even more disturbing:

3+-5

evaluates to -2.

Given the following, I expected

3+*5

to evaluate to 15. Instead it throws an error.

Possible resolution related to thewaywewalk's answer to my previous question at Why is a trailing comma in a cell array valid Matlab syntax?


Solution

  • + and - are not only binary operators, they are also unary operators.

    Documentation:

    http://de.mathworks.com/help/matlab/ref/uplus.html http://de.mathworks.com/help/matlab/ref/uminus.html

    For this reasons, the first two lines are evaluated as 3+(+4) and 3+(-5) but the last fails because no unary multiplication exists.