matlabfunctioncommutativity

Does the order of inputs matter in MATLAB functions?


I have a function called f(x,y), which returns 1 when both x = -1 and y = 1, and 0 otherwise.

I want to apply it on every pair of the same column elements of a matrix. I want to know if I have to repeat it the other way? or does it work the same for f(y,x)? I mean does it return 1 if one of the elements is -1 and the other is 1 anyway or it has to be in order?


Solution

  • It depends on how the function f is defined.

    For example, this is a "symmetric" way to define f:

    function out = f(x,y)
      out = ~(x+y);
    end
    

    And this is an "asymmetric" way:

    function out = f(x,y)
      out = (x == -1) && (y == 1);
    end