0

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?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Free Soul
  • 81
  • 1
  • 2
  • 9
  • 2
    Yes, I’m general the order matters. But whether it matters for this particular function we don’t know unless you show the function. You can do the test yourself: swap the inputs and see if it returns the same value, – Cris Luengo Jul 29 '18 at 14:23

1 Answers1

4

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
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • So what you are saying is that, yes, the order matters, unless the function is written such that it doesn’t. :) – Cris Luengo Jul 29 '18 at 14:24
  • @CrisLuengo ...or as I said in the very beginning - "it depends". :) – Dev-iL Jul 29 '18 at 14:26
  • How to code && in a symmetric way? I'm a bit confused. – Free Soul Jul 29 '18 at 15:21
  • 1
    @FreeSoul If you want it to be "symmetric" just use the top suggestion. If you insist, you can do `out = ((x == -1) && (y == 1)) || ((x == 1) && (y == -1))`. Note that if `x` or `y` are arrays, you need to use `&` instead of `&&`. – Dev-iL Jul 29 '18 at 15:44
  • 1
    I would "insist", because your symmetric example doesn't give the desired result for other input pairs where `x+y==0`, for instance by the original definition `f(-4,4)` should return `0` but would return `1` here. Probably easier to just be explicit with something like `max(x,y) == 1 && min(x,y) == -1`. – Wolfie Jul 30 '18 at 08:17
  • @Wolfie You're right. However, [from a related question by the OP](https://stackoverflow.com/q/51579179/3372061) we learn that this matrix only contains `-1, 0, 1` elements. – Dev-iL Jul 30 '18 at 08:25