"not equal to" in MATLAB

3.030 visualizzazioni (ultimi 30 giorni)
Mobile Computing
Mobile Computing il 14 Giu 2019
Risposto: Prannoy il 4 Giu 2023
How do I use "not equal to" symbol in MATLAB?
not_equal.png
The above code gives an error.
  2 Commenti
Stephen23
Stephen23 il 14 Giu 2019
Modificato: Stephen23 il 14 Giu 2019
When I searched the internet for "MATLAB not equal" this was the first link returned:
which shows all of the element-wise logical operators, with links at the bottom to the relevant pages, e.g.:
Did you try looking at the MATLAB documentation?
Steven Lord
Steven Lord il 14 Giu 2019
FYI for Mobile Computing, the exclamation point (bang) has a different meaning in MATLAB.

Accedi per commentare.

Risposte (5)

Jan
Jan il 14 Giu 2019
Several possible ways:
if 1 ~= 2
if not(1 == 2)
if ~(1==2)
if ne(1, 2)
if ~eq(1, 2)
if 1 - 2 % Implicitly evaluated: all(1 - 2)

Manvi Goel
Manvi Goel il 14 Giu 2019
You can use the ~ instead of ! for not in MATLAB
correct.png
  1 Commento
Guillaume
Guillaume il 14 Giu 2019
"You can use..."
It's not you can. It's you have to.

Accedi per commentare.


Jack Rigby
Jack Rigby il 17 Nov 2020
~=

Richard Payne
Richard Payne il 20 Feb 2021
Is there hack to accept != ?
  1 Commento
Steven Lord
Steven Lord il 21 Feb 2021
No. The ! operator means whatever follows will be passed to the operating system (assuming it is the first part of the command, otherwise it is an error.) You cannot change that definition. The ~ operator means logical negation, and the ~= operator means not equals.
a = ~true
a = logical
0
b = 1 ~= 2
b = logical
1
!date
Sun Feb 21 00:19:13 UTC 2021

Accedi per commentare.


Prannoy
Prannoy il 4 Giu 2023
In MATLAB, the ! operator has a separate meaning. '~' is used instead as a logical NOT operator. This means that "not equal to" will be represented by "~=" in MATLAB. I hope that clears your doubt.

Categorie

Scopri di più su Operators and Elementary Operations in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by