1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab operator question

  1. Jan 25, 2012 #1
    1. The problem statement, all variables and given/known data
    I have a problem with vectors x and y, where x=[-3,0,0,2,5,8] and y=[-5,-2,0,3,4,10]
    The problem asks me to determine z=y<~x
    I've searched my book and cannot determine what this operator is telling me to do. Am I supposed to determine the values of y that are not less than x? I enter this exaclty as stated into matlab and it returns a vector z=[1,1,1,0,0,0] If someone could help me out here, i'd appreciate it.

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Jan 26, 2012 #2


    Staff: Mentor

    ~x is the complement of vector x, which evaluates to a new vector with all the signs switched.

    z = y < ~ x causes each element of z to be set to 0 or 1 if an element of y is less than the corresponding element in ~x.

    From you answer, I'm guessing that the problem is actually z = y <= ~x.
  4. Jan 26, 2012 #3
    That's not what the ~ operator does in MATLAB. The ~ is for logical negation, which only applies to logical arrays. Any true element becomes a false element and vice versa. If x is not a logical array, I believe it is first converted into one before the negation is applied. Thus "~x" will have a 0 for every non-zero element in x, and a 1 for every element of x equal to zero.

    The idea of using the < operator on a logical array seems improper to me.
  5. Jan 26, 2012 #4


    Staff: Mentor

    Your explanation sounds reasonable to me.
    Not to me. You should be able to compare (<, <=, ==, >=, >) two logical values or two arrays of logical values.

    In the OP's question, [-5, -2, 0, 3, 4, 10] is being compared to ~x = ~[-3, 0, 0, 2, 5, 8]. Each nonzero entry in ~x is replaced by 1, and each zero entry is replaced by 0, I believe. Then the two arrays are compared, element by element, with the result array having a 1 where y(i) < ~x(i), and a 0 otherwise. That's my best guess, at any rate.

    I still have a concern about the 3rd element in the answer that the OP posted. I still believe that in the original problem, the comparison is <=, not <.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook