Search code examples
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.

    • If it's symmetric with respect to the inputs, i.e. "one of them" needs to be -1 and "the other" 1, it would work without change for reverse inputs.
    • If the function was defined such that both "the first" input must be -1 and "the second" must be 1 - the result might be different when the argument order is switched.

    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