What Does the MATLAB Operator <~ Do?

  • Thread starter Thread starter Eastonc2
  • Start date Start date
  • Tags Tags
    Matlab Operator
Click For Summary

Discussion Overview

The discussion revolves around the MATLAB operator ~ and its application in the context of comparing two vectors, x and y. Participants are trying to understand the output of the expression z = y <~ x and clarify the behavior of the ~ operator in MATLAB, particularly regarding logical negation and its implications for vector comparison.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the ~ operator, questioning if it indicates values of y that are not less than x.
  • Another participant asserts that ~x represents logical negation, resulting in a vector where non-zero elements of x become 0 and zeros become 1.
  • There is a suggestion that the comparison should actually be z = y <= ~x instead of y <~ x.
  • Some participants discuss the appropriateness of using the < operator with logical arrays, with differing opinions on whether this is proper.
  • One participant agrees with the explanation of ~x but expresses concern about the specific comparison used in the original problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretation of the ~ operator and its application in the expression. There are competing views on whether the comparison should be < or <=, and uncertainty remains regarding the implications of using logical negation in this context.

Contextual Notes

Participants note that the behavior of the ~ operator may depend on whether x is treated as a logical array, which introduces additional complexity to the discussion.

Eastonc2
Messages
19
Reaction score
0

Homework Statement


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 exactly 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.

Homework Equations




The Attempt at a Solution

 
Physics news on Phys.org
Eastonc2 said:

Homework Statement


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 exactly 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.

Homework Equations




The Attempt at a Solution

~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.
 
Mark44 said:
~x is the complement of vector x, which evaluates to a new vector with all the signs switched.

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.
 
MisterX said:
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.
Your explanation sounds reasonable to me.
MisterX said:
The idea of using the < operator on a logical array seems improper 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 <.
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K